Hello Developers,
I am having an issue with my datastore not saving or not properly getting the datastore (I haven’t tested it with a print statement yet to see where the issue is). I have read the code multiple times, however, I can’t seem to find where issue might be, any help will be appreciated! My code: `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)
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 leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local Tix = Instance.new("IntValue")
Tix.Name = "Tix"
local Donation = Instance.new("IntValue")
Donation.Name = "Donated"
local Donated = Instance.new("IntValue")
Donated.Name = "Donated"
local dataStore = DataStoreService:GetDataStore(key)
local dataStore2 = DataStoreService:GetDataStore("Donation")
local orderedDataStore = DataStoreService:GetOrderedDataStore(key)
local orderedDataStore2 = DataStoreService:GetOrderedDataStore("Donation")
local _, pages = safeCall(name, orderedDataStore.GetSortedAsync, orderedDataStore, Enum.DataStoreRequestType.GetSortedAsync, false, 100)
local _, pages2 = safeCall(name, orderedDataStore2.GetSortedAsync, orderedDataStore2, Enum.DataStoreRequestType.GetSortedAsync, false, 100)
local currentPage = pages:GetCurrentPage()
local currentPage2 = pages2: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
Tix.Value = ret
Tix.Parent = leaderstats
leaderstats.Parent = player
break
end
end
else
Tix.Value = 0
Tix.Parent = leaderstats
leaderstats.Parent = player
end
if #currentPage2 > 0 then
for _, dataStoreKey2 in ipairs(currentPage2) do
if not Players:FindFirstChild(name) then return end
dataStoreKey2 = dataStoreKey2.value
local success, ret = safeCall(name, dataStore2.GetAsync, dataStore2, Enum.DataStoreRequestType.GetAsync, dataStoreKey2)
if success then
Donated.Value = ret
Donated.Parent = leaderstats
leaderstats.Parent = player
break
end
end
else
Donated.Value = 0
Donated.Parent = leaderstats
leaderstats.Parent = player
end
end
local function save(player, dontWait)
local userId = player.UserId
local key = “Player_” … userId
local leaderstats = player:FindFirstChild(“leaderstats”)
if leaderstats then
local TixValue = leaderstats:WaitForChild("Tix").Value
local DonationValue = leaderstats:WaitForChild("Donated").Value
local dataStore = DataStoreService:GetDataStore(key)
local dataStore2 = DataStoreService:GetDataStore("Donation")
local orderedDataStore = DataStoreService:GetOrderedDataStore(key)
local orderedDataStore2 = DataStoreService:GetOrderedDataStore("Donation")
local _, pages = safeCall(nil, orderedDataStore.GetSortedAsync, orderedDataStore, Enum.DataStoreRequestType.GetSortedAsync, false, 1)
local _, pages2 = safeCall(nil, orderedDataStore2.GetSortedAsync, orderedDataStore2, Enum.DataStoreRequestType.GetSortedAsync, false, 1)
local latest = pages:GetCurrentPage()[1] or 0
local latest2 = pages2:GetCurrentPage()[1] or 0
local should = (type(latest) == "table" and latest.value or 0) + 1
local should2 = (type(latest2) == "table" and latest2.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 TixValue
end)
safeCall(nil, orderedDataStore2.UpdateAsync, orderedDataStore2, (not dontWait and Enum.DataStoreRequestType.UpdateAsync), should2, function()
return should2
end)
safeCall(nil, dataStore2.UpdateAsync, dataStore2, (not dontWait and Enum.DataStoreRequestType.UpdateAsync), should2, function()
return DonationValue
end)
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, 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)
end
end`