Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Import necessary services
- local DataStoreService = game:GetService("DataStoreService")
- local Players = game:GetService("Players")
- local RunService = game:GetService("RunService")
- -- Create a DataStore for player data
- local dataStore = DataStoreService:GetDataStore("Players")
- -- Define the default player data structure
- local default = {
- SessionLock = false,
- Coins = 0,
- Gems = 0
- }
- -- Define the request type and a function to wait for request budget
- local updateAsync = Enum.DataStoreRequestType.UpdateAsync
- local function waitForRequestBudget()
- local currentBudget = DataStoreService:GetRequestBudgetForRequestType(updateAsync)
- while currentBudget < 1 do
- currentBudget = DataStoreService:GetRequestBudgetForRequestType(updateAsync)
- wait(5)
- end
- end
- -- Function to set up player data when they join
- local function setUp(player)
- local name = player.Name
- local userId = player.UserId
- local key = "Player_" .. userId
- -- Create a leaderstats folder
- local leaderstats = Instance.new("Folder")
- leaderstats.Name = "leaderstats"
- -- Set initial values for Gems and Coins
- local gems = Instance.new("IntValue", leaderstats)
- gems.Name = "Gems"
- gems.Value = 0
- local coins = Instance.new("IntValue", leaderstats)
- coins.Name = "Coins"
- coins.Value = 0
- local success, data, shouldWait
- repeat
- waitForRequestBudget()
- -- Update player data in DataStore
- success = pcall(dataStore.UpdateAsync, dataStore, key, function(oldData)
- oldData = oldData or default
- if oldData.SessionLock then
- if os.time() - oldData.SessionLock < 1800 then
- shouldWait = true
- else
- oldData.SessionLock = os.time()
- data = oldData
- return data
- end
- else
- oldData.SessionLock = os.time()
- data = oldData
- return data
- end
- end)
- if shouldWait then
- task.wait(5)
- shouldWait = false
- end
- until (success and data) or not Players:FindFirstChild(name)
- -- If successful, update leaderstats with stored values
- if success and data then
- for _, v in pairs(data) do
- leaderstats[v.Name] = v
- end
- leaderstats.Parent = player
- end
- end
- -- Function to save player data when they leave
- local function save(player, dontLeave, dontWait)
- local userId = player.UserId
- local key = "Player_" .. userId
- local leaderstats = player:FindFirstChild("leaderstats")
- if leaderstats then
- local coinsValue = leaderstats.Coins.Value -- Corrected to use "Coins" instead of "Cash"
- local success
- repeat
- if not dontWait then
- waitForRequestBudget()
- end
- -- Update player data in DataStore
- success = pcall(dataStore.UpdateAsync, dataStore, key, function()
- return {
- SessionLock = dontLeave and os.time() or nil,
- Coins = coinsValue -- Changed "Cash" to "Coins"
- }
- end)
- until success
- end
- end
- -- Function to save player data on server shutdown
- local function onShutdown()
- if RunService:IsStudio() then
- task.wait(2)
- else
- local finished = Instance.new("BindableEvent")
- local allPlayers = Players:GetPlayers()
- local leftPlayers = #allPlayers
- for _, player in ipairs(allPlayers) do
- coroutine.wrap(function()
- save(player, nil, true)
- leftPlayers -= 1
- if leftPlayers == 0 then
- finished:Fire()
- end
- end)()
- end
- finished.Event:Wait()
- end
- end
- -- Set up existing players
- for _, player in ipairs(Players:GetPlayers()) do
- coroutine.wrap(setUp)(player)
- end
- -- Connect functions to events
- Players.PlayerAdded:Connect(setUp)
- Players.PlayerRemoving:Connect(save)
- game:BindToClose(onShutdown)
- -- Periodically save player data
- while true do
- wait(60)
- for _, player in ipairs(Players:GetPlayers()) do
- coroutine.wrap(save)(player, true)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement