Sorry for bumping, but for some reason Bez’s method refuses to work and ret
is always nil.
local __ISVIP = game:GetService("ReplicatedStorage"):FindFirstChild("__ISVIP")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local function waitForRequestBudget(requestType)
local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
while currentBudget < 1 do
currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
task.wait(5)
end
end
local function safeCall(playerName, func, self, requestType, ...)
local success, ret
repeat
if requestType then
waitForRequestBudget(requestType)
end
success, ret = pcall(func, self, ...)
if not success then
print("Error: " .. ret)
if string.find(ret, "501") or string.find(ret, "504") then
return
end
end
until (success) or (playerName and not Players:FindFirstChild(playerName))
return success, ret
end
local function setUp(player)
local name = player.Name
local userId = player.UserId
local key = "Player_" .. userId
local str = Instance.new("StringValue")
str.Name = "Codes"
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local TIX = Instance.new("IntValue")
TIX.Name = "TIX"
local dataStore = DataStoreService:GetDataStore(key)
local orderedDataStore = DataStoreService:GetOrderedDataStore(key)
local _, pages = safeCall(name, orderedDataStore.GetSortedAsync, orderedDataStore, Enum.DataStoreRequestType.GetSortedAsync, false, 100)
local currentPage = pages:GetCurrentPage()
if #currentPage > 0 then
for _, dataStoreKey in ipairs(currentPage) do
if not Players:FindFirstChild(name) then return end
dataStoreKey = dataStoreKey.value
local success, ret = safeCall(name, dataStore.GetAsync, dataStore, Enum.DataStoreRequestType.GetAsync, dataStoreKey)
if success then
str.Value = ret
TIX.Value = ret
TIX.Parent = leaderstats
str.Parent = player
leaderstats.Parent = player
break
end
end
else
str.Value = ""
str.Parent = player
TIX.Value = 100
TIX.Parent = leaderstats
leaderstats.Parent = player
end
end
local function save(player, dontWait)
if not __ISVIP.Value then
local userId = player.UserId
local key = "Player_" .. userId
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local cashValue = leaderstats.TIX.Value
local strValue = player.Codes.Value
local dataStore = DataStoreService:GetDataStore(key)
local orderedDataStore = DataStoreService:GetOrderedDataStore(key)
local _, pages = safeCall(nil, orderedDataStore.GetSortedAsync, orderedDataStore, Enum.DataStoreRequestType.GetSortedAsync, false, 1)
local latest = pages:GetCurrentPage()[1] or 0
local should = (type(latest) == "table" and latest.value or 0) + 1
safeCall(nil, orderedDataStore.UpdateAsync, orderedDataStore, (not dontWait and Enum.DataStoreRequestType.UpdateAsync), should, function()
return should
end)
safeCall(nil, dataStore.UpdateAsync, dataStore, (not dontWait and Enum.DataStoreRequestType.UpdateAsync), should, function()
return cashValue,strValue
end)
end
end
end
local function onShutdown()
if __ISVIP.Value then return end
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, true)
leftPlayers -= 1
if leftPlayers == 0 then
finished:Fire()
end
end)()
end
finished.Event:Wait()
end
end
Players.PlayerAdded:Connect(setUp)
Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutdown)
for _, player in Players:GetPlayers() do
coroutine.wrap(setUp)(player)
end
while true do
task.wait(60)
for _, player in Players:GetPlayers() do
coroutine.wrap(save)(player)
end
end