Issues with UpdateAsync()

Hey.
I’m trying to save a level datastore when the player leaves:

game.Players.PlayerRemoving:Connect(function(player)
	dsLevel:UpdateAsync(player.UserId, function(oldValue)
		local newValue = oldValue or 0
		newValue = player.leaderstats.Level.Value
		return newValue
	end)
end)

but for some reason, it always saves 0 as the dsLevel.
When I try writing newValue = newValue + 50, it works fine and increases the value saved by 50 every time.

I do not have any idea what function(oldValue) is in line 2 of your script, but I believe that is your problem. Could you provide more detail on it please, so we can help?

Here is an article on datastores to help you understand UpdateAsync and GetAsync:

1 Like

Are you sure this, player.leaderstats.Level.Value isn’t equal to zero?

1 Like

Yes, the value always increases

1 Like

have you checked the server if it changes the value? The client can’t do all of that. Sometimes when we look at the value, we see that it changes in the client, but not always in the server.

1 Like

What value? the datastore level value after it saves?

Level’s Value, you need to make sure it changes in the server, so it can be saved into the games data. The link I provided at the top should help you understand why data stores are server-sided only.

1 Like

Yes it is changing, The value of it is changing in player.leaderstats.Level…

The script you provided should work, i think there is else at play here. First ensure that this is on the server( handled on the server) and for testing purposes place a print(newValue) right before the return in the Update Async function , to see what it prints

1 Like

In the server? Here is a video to help understand:

When testing your game you need to make sure you look at the server-side, not the client-side as @Jaycbee05 and I have been explaining.

1 Like

I tested it, when it was on client it always showed the level of the player, but when I changed it to server it showed 0.
What can I do to fix that?

The print command right before the return is also not working…

use a (server)script to change the value of Level, and do it from there

Here is an AlvinBlox video for better understanding on datastores:

2 Likes

I had to use game:BindToClose because for some reason my cash didn’t save if I was the last player left on the server. If you decide to try that be aware that in studio game:BindToClose will cause 30 seconds of (Not Responding) → BindToClose 100% Infinite Yield

(EDIT): If you don’t want to use BindToClose then I would suggest implementing autosave system that saves the value to datastore when player.leaderstats.Level.Value changes. Use player.leaderstats.Level:GetPropertyChangedSignal("Value"):Connect

1 Like