Chello fellow Dev!
I’ve encountered some issue with my datastore script.
You see, it supposed to save load data, save data and save player data in case of game closed / server crash.
However, it isn’t very consistent and sometime doesn’t save/load player data. As the result, some player can lose their data.
I encountered some other issue in output as well :
Datastore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer request.
Script :
local players = game:GetService("Players")
local datastoreService = game:GetService("DataStoreService")
local playerData = datastoreService:GetDataStore("PlayerData")
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local rank = Instance.new("IntValue")
rank.Name = "Rank"
rank.Value = 0
rank.Parent = leaderstats
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = 0
money.Parent = leaderstats
local currentExp = Instance.new("IntValue")
currentExp.Name = "CurrentExp"
currentExp.Value = 0
currentExp.Parent = leaderstats
local rankExp = Instance.new("IntValue")
rankExp.Name = "RankExp"
rankExp.Value = 0
rankExp.Parent = leaderstats
local data = playerData:GetAsync(player.UserId)
if data ~= nil then
rank.Value = data[1]
money.Value = data[2]
currentExp.Value = data[3]
rankExp.Value = data[4]
elseif data == nil then -- Handle case if player are new to the game
rank.Value = 1
money.Value = 500
currentExp.Value = 0
rankExp.Value = 1000
end
end
players.PlayerAdded:Connect(leaderboardSetup)
local function playerLeave(player)
local rank = player:FindFirstChild("leaderstats").Rank
local money = player:FindFirstChild("leaderstats").Money
local currentExp = player:FindFirstChild("leaderstats").CurrentExp
local rankExp = player:FindFirstChild("leaderstats").RankExp
local save = {}
table.insert(save, rank.Value)
table.insert(save, money.Value)
table.insert(save, currentExp.Value)
table.insert(save, rankExp.Value)
playerData:SetAsync(player.UserId, save)
end
players.PlayerRemoving:Connect(playerLeave)
game:BindToClose(function()
for _,v in ipairs(players:GetPlayers()) do
task.spawn(function()
playerLeave(v)
end)
end
wait(5)
end)