Guest User

StackEdit suck

a guest
May 9th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None | 0 0
  1. -- Set up table to return to any script that requires this module script
  2. local CarStatManager = {}
  3.  
  4. local DataStoreService = game:GetService("DataStoreService")
  5. local carData = DataStoreService:GetDataStore("carData")
  6.  
  7. -- Table to hold player information for the current session
  8. local sessionData = {}
  9.  
  10.  
  11.  
  12. -- Function that other scripts can call to change a player's stats
  13. function CarStatManager:ChangeStat(player, statName, value)
  14.     local playerUserId = "Player_" .. player.UserId
  15.     assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")
  16.     if typeof(sessionData[playerUserId][statName]) == "number" then
  17.         sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
  18.     else
  19.         sessionData[playerUserId][statName] = value
  20.     end
  21. end
  22.    
  23. function CarStatManager:GetStat(player)
  24. --  local success, currentExperience = pcall(function()
  25. --      return carData:GetAsync("Player_"..player.UserId)
  26. --  end)
  27.     return sessionData[player.UserId]["Owned"]
  28. end
  29.  
  30. -- Function to add player to the 'sessionData' table
  31. local function setupPlayerData(player)
  32.     local playerUserId = "Player_" .. player.UserId
  33.     local success, data = pcall(function()
  34.         return carData:GetAsync(playerUserId)
  35.     end)
  36.     if success then
  37.         if data then
  38.             warn(player.Name.." Is in car database.")
  39.             warn("Fetching data for player: "..player.Name)
  40.             -- Data exists for this player
  41.             sessionData[playerUserId] = data
  42.         else
  43.             warn(player.Name.." Haven't played before")
  44.             warn("Setting Default Values for Player: "..player.Name)
  45.             -- Data store is working, but no current data for this player
  46.             sessionData[playerUserId] = {
  47.                 ["Owned"] = {}
  48.             }
  49.         end
  50.     else
  51.         warn("Error: Couldn't fetch Car Data")
  52.     end
  53. end
  54.  
  55. -- Function to save player's data
  56. local function saveCarData(playerUserId)
  57.     if sessionData[playerUserId] then
  58.         carData:SetAsync(playerUserId, sessionData[playerUserId])
  59.     end
  60. end
  61.  
  62. -- Function to save player data on exit
  63. local function saveOnExit(player)
  64.     local playerUserId = "Player_" .. player.UserId
  65.     saveCarData(playerUserId)
  66. end
  67.  
  68. -- Connect 'setupPlayerData()' function to 'PlayerAdded' event
  69. game.Players.PlayerAdded:Connect(setupPlayerData)
  70.  
  71. -- Connect 'saveOnExit()' function to 'PlayerRemoving' event
  72. game.Players.PlayerRemoving:Connect(saveOnExit)
  73.  
  74. return CarStatManager
Advertisement
Add Comment
Please, Sign In to add comment