Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. -- Set up table to return to any script that requires this module script
  2. local PlayerStatManager = {}
  3.  
  4. local DataStoreService = game:GetService("DataStoreService")
  5. local playerData = DataStoreService:GetDataStore("PlayerData")
  6.  
  7. -- Table to hold player information for the current session
  8. local sessionData = {}
  9.  
  10. local AUTOSAVE_INTERVAL = 60
  11.  
  12. -- Function that other scripts can call to change a player's stats
  13. function PlayerStatManager:ChangeStat(player, statName, value)
  14. assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")
  15. local playerUserId = "Player_" .. player.UserId
  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 to add player to the 'sessionData' table
  24. local function setupPlayerData(player)
  25. local playerUserId = "Player_" .. player.UserId
  26. local success, data = pcall(function()
  27. return playerData:GetAsync(playerUserId)
  28. end)
  29. if success then
  30. if data then
  31. -- Data exists for this player
  32. sessionData[playerUserId] = data
  33. else
  34. -- Data store is working, but no current data for this player
  35. sessionData[playerUserId] = {Money=0, Experience=0}
  36. end
  37. else
  38. warn("Cannot access data store for player!")
  39. end
  40. end
  41.  
  42. -- Function to save player's data
  43. local function savePlayerData(playerUserId)
  44. if sessionData[playerUserId] then
  45. local success, err = pcall(function()
  46. playerData:SetAsync(playerUserId, sessionData[playerUserId])
  47. end)
  48. if not success then
  49. warn("Cannot save data for player!")
  50. end
  51. end
  52. end
  53.  
  54. -- Function to save player data on exit
  55. local function saveOnExit(player)
  56. local playerUserId = "Player_" .. player.UserId
  57. savePlayerData(playerUserId)
  58. end
  59.  
  60. -- Function to periodically save player data
  61. local function autoSave()
  62. while wait(AUTOSAVE_INTERVAL) do
  63. for playerUserId, data in pairs(sessionData) do
  64. savePlayerData(playerUserId)
  65. end
  66. end
  67. end
  68.  
  69. -- Start running 'autoSave()' function in the background
  70. spawn(autoSave)
  71.  
  72. -- Connect 'setupPlayerData()' function to 'PlayerAdded' event
  73. game.Players.PlayerAdded:Connect(setupPlayerData)
  74.  
  75. -- Connect 'saveOnExit()' function to 'PlayerRemoving' event
  76. game.Players.PlayerRemoving:Connect(saveOnExit)
  77.  
  78. return PlayerStatManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement