Advertisement
SamIsWicked

Untitled

Apr 10th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. local dataService = {}
  2.  
  3. local DataStoreService = game:GetService("DataStoreService")
  4. local ServerStorage = game:GetService("ServerStorage")
  5. local Players = game:GetService("Players")
  6. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  7.  
  8. local dataStorage = DataStoreService:GetDataStore("PlayerData")
  9.  
  10. function setDataAsync(player)
  11. local playerDataFolder = ServerStorage.PlayerData[Players:GetNameFromUserIdAsync(player.UserId)]
  12. local cash = playerDataFolder.Cash
  13. local gameTime = playerDataFolder.Time
  14.  
  15. local success, errorMessage = pcall(function()
  16. dataStorage:SetAsync(player.UserId .. " Cash", cash.Value)
  17. dataStorage:SetAsync(player.UserId .. " Time", gameTime.Value)
  18. end)
  19.  
  20. if errorMessage then
  21. warn(errorMessage)
  22. end
  23. end
  24.  
  25. function dataService.Init()
  26.  
  27. Players.PlayerRemoving:Connect(setDataAsync)
  28.  
  29. game:BindToClose(function()
  30.  
  31. for i, player in pairs(game.Players:GetPlayers()) do
  32. setDataAsync(player)
  33. end
  34. end)
  35.  
  36. Players.PlayerAdded:Connect(function(player)
  37. local playerFolder = Instance.new("Folder")
  38. playerFolder.Name = player.Name
  39. playerFolder.Parent = ServerStorage:WaitForChild("PlayerData")
  40.  
  41. local cash = Instance.new("IntValue")
  42. cash.Name = "Cash"
  43. cash.Parent = playerFolder
  44. cash.Value = 50
  45.  
  46. local gameTime = Instance.new("IntValue")
  47. gameTime.Name = "Time"
  48. gameTime.Parent = playerFolder
  49. gameTime.Value = 0
  50.  
  51. local leaderstats = Instance.new("Folder")
  52. leaderstats.Name = "leaderstats"
  53. leaderstats.Parent = player
  54.  
  55. local cashLS = Instance.new("IntValue")
  56. cashLS.Name = "Cash"
  57. cashLS.Parent = leaderstats
  58.  
  59. local cashData = nil
  60. local timeData = nil
  61.  
  62. local success, errorMessage = pcall(function()
  63. cashData = dataStorage:GetAsync(player.UserId .. " Cash")
  64. timeData = dataStorage:GetAsync(player.UserId .. " Time")
  65. end)
  66.  
  67. if errorMessage then
  68. player:Kick("There has been an issue with loading your Data, please rejoin after a few minutes.")
  69. end
  70.  
  71. cash.Value = cashData or 0
  72. cashLS.Value = cash.Value
  73.  
  74. gameTime.Value = timeData or 0
  75.  
  76. while wait(1) do
  77. gameTime.Value += 1
  78. end
  79. end)
  80. end
  81.  
  82. return dataService
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement