Advertisement
Richbadniss

Updated DataStore

Dec 21st, 2023
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. -- Import necessary services
  2. local DataStoreService = game:GetService("DataStoreService")
  3. local Players = game:GetService("Players")
  4. local RunService = game:GetService("RunService")
  5.  
  6. -- Create a DataStore for player data
  7. local dataStore = DataStoreService:GetDataStore("Players")
  8.  
  9. -- Define the default player data structure
  10. local default = {
  11.     SessionLock = false,
  12.     Coins = 0,
  13.     Gems = 0
  14. }
  15.  
  16. -- Define the request type and a function to wait for request budget
  17. local updateAsync = Enum.DataStoreRequestType.UpdateAsync
  18. local function waitForRequestBudget()
  19.     local currentBudget = DataStoreService:GetRequestBudgetForRequestType(updateAsync)
  20.  
  21.     while currentBudget < 1 do
  22.         currentBudget = DataStoreService:GetRequestBudgetForRequestType(updateAsync)
  23.         wait(5)
  24.     end
  25. end
  26.  
  27. -- Function to set up player data when they join
  28. local function setUp(player)
  29.     local name = player.Name
  30.     local userId = player.UserId
  31.     local key = "Player_" .. userId
  32.  
  33.     -- Create a leaderstats folder
  34.     local leaderstats = Instance.new("Folder")
  35.     leaderstats.Name = "leaderstats"
  36.  
  37.     -- Set initial values for Gems and Coins
  38.     local gems = Instance.new("IntValue", leaderstats)
  39.     gems.Name = "Gems"
  40.     gems.Value = 0
  41.  
  42.     local coins = Instance.new("IntValue", leaderstats)
  43.     coins.Name = "Coins"
  44.     coins.Value = 0
  45.  
  46.     local success, data, shouldWait
  47.     repeat
  48.         waitForRequestBudget()
  49.         -- Update player data in DataStore
  50.         success = pcall(dataStore.UpdateAsync, dataStore, key, function(oldData)
  51.             oldData = oldData or default
  52.             if oldData.SessionLock then
  53.                 if os.time() - oldData.SessionLock < 1800 then
  54.                     shouldWait = true
  55.                 else
  56.                     oldData.SessionLock = os.time()
  57.                     data = oldData
  58.                     return data
  59.                 end
  60.             else
  61.                 oldData.SessionLock = os.time()
  62.                 data = oldData
  63.                 return data
  64.             end
  65.         end)
  66.  
  67.         if shouldWait then
  68.             task.wait(5)
  69.             shouldWait = false
  70.         end
  71.     until (success and data) or not Players:FindFirstChild(name)
  72.  
  73.     -- If successful, update leaderstats with stored values
  74.     if success and data then
  75.         for _, v in pairs(data) do
  76.             leaderstats[v.Name] = v
  77.         end
  78.         leaderstats.Parent = player
  79.     end
  80. end
  81.  
  82. -- Function to save player data when they leave
  83. local function save(player, dontLeave, dontWait)
  84.     local userId = player.UserId
  85.     local key = "Player_" .. userId
  86.  
  87.     local leaderstats = player:FindFirstChild("leaderstats")
  88.  
  89.     if leaderstats then
  90.         local coinsValue = leaderstats.Coins.Value  -- Corrected to use "Coins" instead of "Cash"
  91.         local success
  92.  
  93.         repeat
  94.             if not dontWait then
  95.                 waitForRequestBudget()
  96.             end
  97.             -- Update player data in DataStore
  98.             success = pcall(dataStore.UpdateAsync, dataStore, key, function()
  99.                 return {
  100.                     SessionLock = dontLeave and os.time() or nil,
  101.                     Coins = coinsValue  -- Changed "Cash" to "Coins"
  102.                 }
  103.             end)
  104.         until success
  105.     end
  106. end
  107.  
  108. -- Function to save player data on server shutdown
  109. local function onShutdown()
  110.     if RunService:IsStudio() then
  111.         task.wait(2)
  112.     else
  113.         local finished = Instance.new("BindableEvent")
  114.         local allPlayers = Players:GetPlayers()
  115.         local leftPlayers = #allPlayers
  116.  
  117.         for _, player in ipairs(allPlayers) do
  118.             coroutine.wrap(function()
  119.                 save(player, nil, true)
  120.                 leftPlayers -= 1
  121.                 if leftPlayers == 0 then
  122.                     finished:Fire()
  123.                 end
  124.             end)()
  125.         end
  126.  
  127.         finished.Event:Wait()
  128.     end
  129. end
  130.  
  131. -- Set up existing players
  132. for _, player in ipairs(Players:GetPlayers()) do
  133.     coroutine.wrap(setUp)(player)
  134. end
  135.  
  136. -- Connect functions to events
  137. Players.PlayerAdded:Connect(setUp)
  138. Players.PlayerRemoving:Connect(save)
  139. game:BindToClose(onShutdown)
  140.  
  141. -- Periodically save player data
  142. while true do
  143.     wait(60)
  144.     for _, player in ipairs(Players:GetPlayers()) do
  145.         coroutine.wrap(save)(player, true)
  146.     end
  147. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement