Datastore Problem (data disappearing)

Can Anyone tell me what is wrong with my script? When the player joins their saved Timeplayed number flashes on the leaderboard then resets to 0. It is super annoying.

Problem Script Below.

Add time script (just in case this is part of the problem)

Test Place: 2.2k + 10% - Roblox

  1. Do PlayerAdded and PlayerRemoving instead of ChildAdded and ChildRemoved.
  2. It’s Player.UserId not Player.userId.
  3. You need to wrap the GetAsync and SetAsync in a pcall.
  4. Capitlize connect, so it should be Connect.
1 Like

Follow the above, great advice there. But adding onto that, you kinda have two pieces of code colliding/ disagreeing.

Both the code handling the loading of player data (top script) and your function onPlayerEntered are both creating different leaderstats. The latter one in your post, always starting at 0 minutes, and the other lining up with the last saved value.

I agree with @Quwanterz’s steps on making the script more efficient and organized; though I am pretty sure setting the IntValue’s Value to 0 in the second script, WILL override the first script’s “:GetAsync()”.

My reasoning for this is that I am pretty sure that because the first-listed script includes an “event-triggered function”, it is a teeny bit faster than the second script. (Correct me if I am wrong.) Because of this, I come to the conclusion that setting “mins.Value = 0” in the second script comes just hundredths of a second after you set the value as the player’s “datastore” for the key given.

This may be why you see the value flash as the recent datastore for less than a second, and then go to 0.

Basically, remove the instance creation + parenting & value being set in the second script, as it is redundant since there is already the IntValue created. Just replace it with something like:

repeat wait() until newPlayer:FindFirstChild("leaderstats")
-- And then the while loop here.

Reply if this works or not, I hope this helped give you a better understanding of correct syntax usage.

Edit: If this did work, don’t forget to make this the solution, to let others know how to fix the error!

I agree especially with the “userId” syntax mistake, though I think that it may be a different problem causing this error, as they say the recently saved number shows up on the leaderboard, then turns to 0.