How to make starting leaderstat points

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.

Copy the error from output and paste it here. Probably this is not the path, this doesnt exist:
Player.leaderstats.Points.Value

Errors are your friends xD do not just say “infinite yield on the entire script” you should carefully read the error/warning

03:37:34.495 Script Testing @ 22 May 2023 03:37 auto-recovery file was created - Studio
03:37:36.034 ServerScriptService.PointsSystem:32: Expected ‘)’ (to close ‘(’ at column 6), got ‘to’ - Studio - PointsSystem:32
03:37:42.971 Infinite yield possible on ‘Players.JackyssWrld:WaitForChild(“leaderstats”)’ - Studio
03:37:42.971 Stack Begin - Studio
03:37:42.971 Script ‘Players.JackyssWrld.PlayerGui.TicketShop.MainFrame.Item1.LocalScript’, Line 10 - Studio - LocalScript:10
03:37:42.971 Stack End - Studio
03:37:42.971 Infinite yield possible on ‘Players.JackyssWrld:WaitForChild(“leaderstats”)’ - Studio
03:37:42.972 Stack Begin - Studio
03:37:42.972 Script ‘Players.JackyssWrld.PlayerGui.TicketShop.MainFrame.Item1.LocalScript’, Line 10 - Studio - LocalScript:10
03:37:42.972 Stack End - Studio
03:37:42.972 Infinite yield possible on ‘Players.JackyssWrld:WaitForChild(“leaderstats”)’ - Studio
03:37:42.972 Stack Begin - Studio
03:37:42.972 Script ‘Players.JackyssWrld.PlayerGui.TicketShop.MainFrame.Item1.LocalScript’, Line 10 - Studio - LocalScript:10
03:37:42.972 Stack End - Studio
03:37:42.972 Infinite yield possible on ‘Players.JackyssWrld:WaitForChild(“leaderstats”)’ - Studio
03:37:42.973 Stack Begin - Studio
03:37:42.973 Script ‘Players.JackyssWrld.PlayerGui.TicketShop.MainFrame.Item1.LocalScript’, Line 10 - Studio - LocalScript:10
03:37:42.973 Stack End - Studio
03:37:42.973 Infinite yield possible on ‘Players.JackyssWrld:WaitForChild(“leaderstats”)’ - Studio
03:37:42.973 Stack Begin - Studio
03:37:42.973 Script ‘Players.JackyssWrld.PlayerGui.TicketShop.MainFrame.Item1.LocalScript’, Line 10 - Studio - LocalScript:10