I am trying to make a rank system with points and I am working on the datastore part. It saves every 10 seconds due to datastore queue systems. I want the data to save as often as possible so the player can have their rank/points without loosing any progress.
So when I join the game, the loop will wait 10 seconds and save the data. It will also check if the player is still in the game. If not, it will break and move on.
If the player leaves, it will save the data.
If the server gets shutdown, everyones data will save.
The problem is that my output says that the data is saved (reference the photo below) but when I rejoin, the data is still 0 or nil.
Code
local datastore = game:GetService("DataStoreService")
local d1 = datastore:GetDataStore("PointsStore")
local d2 = datastore:GetDataStore("RankStore")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "Points"
folder.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = folder
local rank = Instance.new("StringValue")
rank.Name = "Rank"
rank.Parent = folder
local function saveData(player)
local success, err = pcall(function()
d1:SetAsync(player.UserId, points.Value)
d2:SetAsync(player.UserId, rank.Value)
end)
if success then
print("Data has been saved!")
else
print("Data hasn't been saved!")
warn(err)
end
end
while wait(10) and game.Players:FindFirstChild(player.Name) do
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
game.Players.PlayerRemoving:Connect(function()
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)
end)
I am also not getting any error showing any queue warnings or anything? I don’t get it. Whats wrong?
Ok, so your code had a few problems. First, I wouldn’t wrap a pcall in a pcall. If you want to see if your save function ran successfully, return the success and error values from the pcall in your function. Also, you should connect to each event separately and not all in the PlayerAdded event. Saving every ten seconds is a lot, every minute should do and task.wait() should be used. Lastly, you shouldn’t use wait as a loop condition. Here’s the modified code:
local datastore = game:GetService("DataStoreService")
local d1 = datastore:GetDataStore("PointsStore")
local d2 = datastore:GetDataStore("RankStore")
local function saveData(player)
local success, err = pcall(function()
d1:SetAsync(player.UserId, player.Points.Points.Value)
d2:SetAsync(player.UserId, player.Points.Rank.Value)
end)
if success then
print("Data has been saved!")
else
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "Points"
folder.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = folder
local rank = Instance.new("StringValue")
rank.Name = "Rank"
rank.Parent = folder
while game.Players:FindFirstChild(player.Name) do
task.wait(60)
saveData(player)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
saveData(player)
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
saveData(player)
end
end)
To further improve your code you need to load the player’s data when they join. Right now, a player’s data is just overwritten whenever they join. I would also suggest using one datastore and saving the data in a table.
One more thing. I am getting a warning for a datastore overload, and which the data has been put into queue. After I rejoin, my data is still nil/0 on both values?
DataStore request was added to queue. If request queue fills, further requests will be dropped.
As I’ve said, you aren’t loading the data. So, every time you join the game your data is overwritten. But, the data is indeed saving. I’ve tested the code and haven’t ran into it going over the limits. But, what I assume is happening is the data is saving when you leave the game and during the autosave within 6 seconds. SetAsync has a cooldown of six seconds between requests which causes the request to be added to a queue. To fix this, you should add in a check and only save when the data has changed. In order to finish your data saving system, I would recommend reading this article on datastores. The best way to learn is by trying things on your own.