Reply to horvathmf:
Heres the code in ServerScriptService:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local dataStore = DataStoreService:GetDataStore("Players")
local default = {
SessionLock = false,
Cash = 0
}
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
local function setUp(player)
local name = player.Name
local userId = player.UserId
local key = "Player_" .. userId
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue")
cash.Name = "Cash"
local success, data, shouldWait
repeat
waitForRequestBudget()
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 success and data then
cash.Value = data.Cash
cash.Parent = leaderstats
leaderstats.Parent = player
end
end
local function save(player, dontLeave, dontWait)
local userId = player.UserId
local key = "Player_" .. userId
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local cashValue = leaderstats.Cash.Value
local success
repeat
if not dontWait then
waitForRequestBudget()
end
success = pcall(dataStore.UpdateAsync, dataStore, key, function()
return {
SessionLock = dontLeave and os.time() or nil,
Cash = cashValue
}
end)
until success
end
end
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
for _,player in ipairs(Players:GetPlayers()) do
coroutine.wrap(setUp)(player)
end
Players.PlayerAdded:Connect(setUp)
Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutdown)
while true do
wait(60)
for _,player in ipairs(Players:GetPlayers()) do
coroutine.wrap(save)(player, true)
end
end
2 comments
Whats your leaderstats script?
this is in ServerScriptService: local DataStoreService = game:GetService("DataStoreService") local Players = game:GetService("Players") local RunService = game:GetService("RunService") local dataStore = DataStoreService:GetDataStore("Players") local default = { SessionLock = false, Cash = 0 } local updateAsync = Enum.DataStoreRequestType.UpdateAsync local function waitForRequestBudget() local