SetAsync not working

Hi, today DataStore:SetAsync() randomly stopped working. It doesn’t produce an error, it doesn’t do anything really. Here is my script.

game.Players.PlayerRemoving:Once(function(player)
	local sucess, errormsg = pcall(function()
		print(tonumber(player.leaderstats.Tuba.Value))
		checkpointstore:SetAsync(player.UserId,tonumber(player.leaderstats.Tuba.Value))
	end)
	if sucess then
		print("Sucessfully saved "..player.Name.."'s Data")
	else
		warn(errormsg)
	end
end)

For some context (if needed) the number it prints is 5 but when I rejoin the game it has the previous number aka 4

Might have something to do with the use of :Once(), with the function disconnecting itself after a single use. Maybe try switching it to :Connect()

1 Like

Yeah that might be the issue, that means only one player’s data will save when someone leaves the game. And why do you have to use tonumber()?

I don’t think it’s SetAsync() itself, but what I think it could possibly be is this line here

tonumber(player.leaderstats.Tuba.Value)

You’re using a tonumber, which is not really necessary. What you could do is make sure that the leaderstats are loaded first. Basically just if not leaderstats then return end

Then make sure that you have the value locally. Tuba = (Wherever this goes. Leaderstats most likely)

Than you can do this

checkpointstore:SetAsync(player.UserID, Tuba.Value)

But if really needed, try putting print statements in between just to make sure.

But also pointing out from this

Might have something to do with the use of :Once(), with the function disconnecting itself after a single use. Maybe try switching it to :Connect()

I agree with them. Change game.Players.PlayerRemoving:Once(function(player)) to game.Players.PlayerRemoving:Connect(function(player))

1 Like

i would also recommend using game:BindToClose().

3 Likes

I just tried seeing if that was an issue. Before when I didn’t use tonumber it still didn’t work.

1 Like

Same thing, I was using :Connect() before but switched to :Once() to see if it was the problem. Removed tonumber and changed :Once() to :Connect(), didn’t fix.

This worked. I still don’t know what the issue was.

game:BindToClose(function()
	local sucess, errormsg = pcall(function()
		checkpointstore:SetAsync(plr.UserId,plr.leaderstats.Tuba.Value)
	end)
	if sucess then
		print("Sucessfully saved "..plr.Name.."'s Data")
	else
		warn(errormsg)
	end
end)
1 Like

It was the once call .. and here is for sure.

--ServerScript in ServerScriptService
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local checkpointstore = DataStoreService:GetDataStore("Checkpoints")

local function save(player)
    checkpointstore:UpdateAsync(player.UserId,function()
        return player.leaderstats.Tuba.Value
    end)
end

Players.PlayerRemoving:Connect(save)

game:BindToClose(function()
    for _,player in Players:GetPlayers() do
        save(player)
    end
    task.wait(2)
end)

Sorry was still putting it together.. Why waste it.

1 Like

the server was closed, deleted from existence, before it could save any data. basically, when a server is 1 player when said 1 player leaves the server gets separation anxiety and dies, before the data can save

2 Likes

That’s a good point. Thanks for sharing.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.