Leaderboard doesn't save

I’m currently trying to make a Script that autosaves the leaderboard, I’ve already looked at dozens of tutorials on how to do that, but the Data Stores don’t save. I think I should mention that the leaderboard is a counter that’s reset on death.

This is my code:

local counting = true
local DSS = game:GetService("DataStoreService")
local leaderstore = DSS:GetDataStore("leaderstats")

game.Players.PlayerAdded:Connect(function(plr)
    local Players = game:GetService("Players")
	local leaderstats = Instance.new("Folder")

	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local time = Instance.new("IntValue")
	time.Name = "Time"
	if leaderstore:GetAsync(plr.UserId.."-time") ~= nil then
		time.Value = 0
	end
	time.Parent = leaderstats

	local function counter()
		while counting do
			wait(1)
			time.Value = time.Value + 1
		end
	end
	spawn(counter()))
	local data
	local success, errormsg = pcall(function()
		data = leaderstore:GetAsync(plr.UserId.."-time")
	end)


	if success then
		time.Value = data
	else
		warn("An error occured while loading player data:")
		warn(errormsg)
	end


end)


game.Players.PlayerRemoving:Connect(function(plr)

	local success, errormessage = pcall(function()
		leaderstore:SetAsync(plr.UserId.."-time", plr.leaderstats.Time.Value)
	end)


	if success then
		print("Player data saved!")
	else
		warn("There was an error saving the playerdata!")
		error(errormessage)
	end


end)

What happens, does it not load the data?
If so, on one of the lines you verify whether the user’s data exists:

if leaderstore:GetAsync(plr.UserId.."-time") ~= nil then

But then you set the time’s value to equal 0, you might have meant to set it to equal that saved value:

local savedTime = leaderstore:GetAsync(plr.UserId.."-time")
if savedTime then
    time.Value = savedTime
end

Yeah, it always outputs

Player data saved!

when I exit the test and doesn’t output anything when I join even though it should.

wait() is deprecated, you should use task.wait() instead

PlayerRemoving

Is really inconsistent in Studio, try using

game:BindToClose(function()
    -- save data
end)

Or test in the Roblox player

is enable studio ascess to api enabled

Yes, it is.
ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

The issue is this:
If so, on one of the lines you verify whether the user’s data exists:

if leaderstore:GetAsync(plr.UserId.."-time") ~= nil then

But then you set the time’s value to equal 0, you might have meant to set it to equal that saved value:

local savedTime = leaderstore:GetAsync(plr.UserId.."-time")
if savedTime then
    time.Value = savedTime
end

Now, later on you try to set the time to a loaded data, but the code will never reach that point.
spawn() (though you should use task.spawn()) takes a function, you did not give it a function memory, but rather called the function, thus yielding the next piece of code that loads the data.
Either call spawn like this:

task.spawn(counter)

Or do the loop in the end of the function.

This should work (I also optimized a few things in the code):

local counting = true
local DSS = game:GetService("DataStoreService")
local leaderstore = DSS:GetDataStore("leaderstats")

game.Players.PlayerAdded:Connect(function(plr)
    local Players = game:GetService("Players")
	local leaderstats = Instance.new("Folder")

	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local time = Instance.new("IntValue")
	time.Name = "Time"

    local savedTime = leaderstore:GetAsync(plr.UserId.."-time")
    time.Value = savedTime or 0
	time.Parent = leaderstats

    -- // Just do all the rest of the stuff before the loop.
	while counting do
		task.wait(1)
		time.Value = time.Value + 1
	end
end)


game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormessage = pcall(function()
		leaderstore:SetAsync(plr.UserId.."-time", plr.leaderstats.Time.Value)
	end)


	if success then
		print("Player data saved!")
	else
		warn("There was an error saving the playerdata!")
		error(errormessage)
	end
end)
2 Likes

try using

leaderstore:UpdateAsync(--[[blah blah values here ]])

I still need the plr variable though.

you can refer to this
https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

Thank you so much, this works now!

1 Like