How to make starting leaderstat points

Hi, so I have created a working points system but the only thing missing that I want is when someone joins the game for the first time, I want them to start off with a specific amount of points. Here is the script and some info on it.

This script is in ServerScriptService.

local DataStoreService = game:GetService("DataStoreService")
local PointsStore = DataStoreService:GetDataStore("PointsStore")

game.Players.PlayerAdded:Connect(function(Player)
	local SavedPoints = PointsStore:GetAsync(Player.UserId)

	local Leaderstats = Player:FindFirstChild("leaderstats") or Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local PointsValue = Leaderstats:FindFirstChild("Points") or Instance.new("IntValue", Leaderstats)
	PointsValue.Name = "Points"
	

	if SavedPoints ~= nil then
		PointsValue.Value = SavedPoints
	end

	PointsValue.Changed:Connect(function(NewPoints)
		PointsStore:SetAsync(Player.UserId, NewPoints)
	end)
end)

If you need any more information please ask, if you can find a solution I would really appreciate it!

1 Like
	if SavedPoints ~= nil then
		PointsValue.Value = SavedPoints
	else
		PointsValue.Value = 10 -- your starter value
	end

But, if you let me give a suggestion, you should edit the script, and wrap the GetAsync and SetAsync into pcalls, otherwise your script wont know when Datastore API fails.

And another suggestion, do not save (SetAsync) everytime the value changes, that will quickly cause an error due to many tries of saving the same key on DS

1 Like

To add to this, use the SetAsync() function in a Player.PlayerRemoved function so you only have to do it once instead of every time their points change.

How would I add the pcalls into the script? Sorry, I haven’t worked much with datastores and all that.

local DataStoreService = game:GetService("DataStoreService")
local PointsStore = DataStoreService:GetDataStore("PointsStore")

game.Players.PlayerAdded:Connect(function(Player)
	local success, SavedPoints = pcall(function()
		return PointsStore:GetAsync(Player.UserId)
	end)
	
	local Leaderstats = Player:FindFirstChild("leaderstats") or Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local PointsValue = Leaderstats:FindFirstChild("Points") or Instance.new("IntValue", Leaderstats)
	PointsValue.Name = "Points"

	if success then
		if SavedPoints then
			warn("player has data")
			PointsValue.Value = SavedPoints
		else
			warn("player is new")
			PointsValue.Value = 10 -- your starter value
		end
	else
		warn("DSS failed")
	end
end)
1 Like

Thank you so much! This seems to be working perfectly.

1 Like

Good to hear!
Remember to create the pcall for SetAsync on PlayerRemoving to save the data

Doesn’t it already save or do I need to do something similar to what this person did: How to save points even when leave - #42 by HenryThe_Legendary

Never mind, I believe I got it. I put this underneath your code:

game.Players.PlayerRemoving:Connect(function(Player)
	local success, SavedPoints = pcall(function()
		return pointsStore:SetAsync(Player.UserId)
	end)
	
	local Leaderstats = Player:FindFirstChild("leaderstats") or Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local PointsValue = Leaderstats:FindFirstChild("Points") or Instance.new("IntValue", Leaderstats)
	PointsValue.Name = "Points"
	
	if success then
		if SavedPoints then
			warn("player has data")
			PointsValue.Value = SavedPoints
		end
	else
		warn("DSS failed")
	end
	
	
end)

If this looks wrong please let me know.

Should be like this:

game.Players.PlayerRemoving:Connect(function(Player)
	local stuffToSave = Player.leaderstats.Points.Value
	
	local success, err = pcall(function()
		return PointsStore:SetAsync(Player.UserId, stuffToSave)
	end)

	if success then
		warn("saved succsess")
	else
		warn("Saving DSS failed", err)
	end
end)

But, thats only the basics, you probably dont want to save only 1 value into player’s datastore, maybe you wish to save a table made of many values.
For starting read that code, understand it, and you will be able to scalate it.

SetAsync on pcall will return if it successfully saved or not, along with the error message of why it failed.
You should provide what to save, in this case only the value that player has, or could be a table you should build when player’s leaving, by reading the values the player has

1 Like

Thank you for your help, this has really helped me get a better understanding of how datastores work along with pcall which I never knew about before today.

1 Like

Actually hold up, for some reason when I go into the game, spend some of the points then leave it doesn’t save anymore.

Did you make sure to remove the original SetAsync()?

Yeah, I removed the one I made and replaced it with the one they sent me.

You sure you are changing the value from a ServerScript and not a Client one?

Here is the entire script put together:

local DataStoreService = game:GetService("DataStoreService")
local pointsStore = DataStoreService:GetDataStore("pointsStore")

game.Players.PlayerAdded:Connect(function(Player)
	local success, SavedPoints = pcall(function()
		return pointsStore:GetAsync(Player.UserId)
	end)

	local Leaderstats = Player:FindFirstChild("leaderstats") or Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local PointsValue = Leaderstats:FindFirstChild("Points") or Instance.new("IntValue", Leaderstats)
	PointsValue.Name = "Points"

	if success then
		if SavedPoints then
			warn("player has data")
			PointsValue.Value = SavedPoints
			
		else
			warn("player is new")
			PointsValue.Value = 50 
		end
	else
		warn("DSS failed")
	end

end)

game.Players.PlayerRemoving:Connect(function(Player)
	local stuffToSave = Player.leaderstats.Points.Value

	local success, err = pcall(function()
		return pointsStore:SetAsync(Player.UserId, stuffToSave)
	end)

	if success then
		warn("saved succsess")
	else
		warn("Saving DSS failed", err)
	end
end)

Yes, it’s in a serverscript in serverscriptservice.

I think we’ll need to see the code the changes the players points, everything there looks right

1 Like

Lets debug with prints, add this line in the Removing function:

local stuffToSave = Player.leaderstats.Points.Value
warn(stuff to save:, stuffToSave)

When you leave the game check output and tell us what it prints

I put it in and it put an infinite yield on the entire script, removing the leaderstats entirely.