Data Store doesn't work (please help me)

Please help im new to scripting and im need this script but its doesnt work for me.
what im trying to do is save Points data.
please fix the script, im trying fix it for hours.
also im new to scripting so please be understandable.

leaderstats(script)

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Stage = Instance.new("IntValue")
	Stage.Name = "Stage"
	Stage.Value = 1
	Stage.Parent = leaderstats

	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Value = 0
	Points.Parent = leaderstats
end)

SaveData(script)

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait(0.1)
	local plrkey = "id_"..plr.userId
	local save1 = plr.leaderstats.Points
	
	local GetSaved = ds:GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
	else
		local NumberForSaving = {save1.Value}
		ds:GetAsync(plrkey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds.SetAsync("id_"..plr.userId, {plr.leaderstats.Points.Value})
end)
2 Likes

Has anything came out in the output? If so what has and is there any errors in the script?

Do you have your API service enabled in your settings cause you will need it to save data it is right here just go to the top of the roblox studio and press settings then set it on.

1/ Check your Game setting to see if “Enabled Studio Access to API services” is enabled.

2/ The reason why it doesn’t appear to work is because when the game exit, the game shuts down too fast. So it doesn’t get a chance to save. In addition to “PlayerRemoving” event, you also need to do a BindToClose that gives the game more time save the data.

Check out this page for more detail.

Firstly,
This should not be two seperate scripts, as it will cause errors if the script that creates the leaderstats runs after the datastore script. Combine them as that’s the first issue that might occur.

Secondly,
Wrap your calls in a pcall (protected call) as you can then handle error messages by ex. attempting to save their data if it fails (this is not needed for GetAsync(), but have a pcall though, you just don’t need to check if it fails as that means that the datastores are down, and you cannot attempt to get their data if it’s down.)