Datastore Not Saving Info

Datastore Not Storing Info

I’ve created a datastore to save the time it took a player to complete a level. The datastore has failed to save anything after multiple attempts.

I’ve tried multiple solutions like changing the names of things which didn’t have much effect on anything.

The script is server sided and it is located in the ServerScriptService

Here is the script:

PB = game:GetService("DataStoreService"):GetDataStore("PB")

function PlayerEntered(player)
	repeat wait(1) until player.Character
	
	local TimeTrial = Instance.new("Folder")
	TimeTrial.Name = "TimeTrial"
	TimeTrial.Parent = player

	local PersonalBest = Instance.new("StringValue")
	PersonalBest.Name = "PersonalBest"
	PersonalBest.Parent = TimeTrial
	PersonalBest.Value = "0.00"
	
	if PB:GetAsync("Points_"..player.Name) ~= nil then
		PersonalBest.Value = PB:GetAsync("Points_"..player.Name)
	else
		PersonalBest.Value = '0.00'
	end

	PersonalBest.Changed:connect(function(Val)
		PB:SetAsync("Points_"..player.Name, Val)
	end)

end

game.Players.PlayerAdded:connect(PlayerEntered)

Thanks a lot if you could help <3

Couple things unrelated:
You should only save data every 60 seconds or so and upon the player leaving.
You get the datastore 2 times when you could just get it once.
Always use userid for datastores.
Is the function for setAsync actually running?

It’ll only save whenever PersonalBest changes from it’s value. Also save data via the user id, not the name since if the player changes their name, you’ll end up loading up a new data for them.

I figured to use UserId rather than the player names. I have so when the player reaches the end of the level it changes the value of PersonalBest. It just won’t save to the datastore.
Thanks for the help btw

I just noticed that you used .Changed. The variable passed to the connected function is the name of the property changed, not the value.
edit: nvm, that doesn’t apply to values

Are you sure that PersonalBest is changed?

@Pokemoncraft5290

No, for string values, it is the new value.

I’ve actually have realized that this script was saving the data locally. Thanks for the help though… I’m so sorry