Advertisement
Guest User

Untitled

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