How to make starting leaderstat points

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

That’s the entire output after putting that line of code where you said to.

PointsSystem:32: Expected ‘)’ (to close ‘(’ at column 6)

Probably didnt copy the entire line, you missed a " ) "

You have this:
warn("stuff to save:", stuffToSave

instead of this:
warn("stuff to save:", stuffToSave)

03:47:26.979 player has data - Server - PointsSystem:17
03:47:40.265 stuff to save: 20 - Server - PointsSystem:32

This is all it shows now that I fixed that bit.

Yeah, after enclosing the () properly now it prints the value that is going to save, which is 20

Yet it still doesn’t save even though it says it’s going to save the amount I leave with.

I tested it and works as intended on my side. I joined, I got 50, I changed that to 80, got the print of 80, I rejoined and I got 80 as expected

Im using this script:

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
	warn("stuff to save:", stuffToSave)

	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)

I got mine to finally work by changing the datastore so instead of it being pointsStore it is now pointsstore. Must have been a weird glitch.

1 Like

Wait, never mind. it only saved once. I rejoined the game and now it won’t save anymore. @Dev_Peashie

Maybe your studio is closing fast the testPlay and not having enough time to save the datastore. Try this code:

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("players data in DS:", SavedPoints)
			PointsValue.Value = SavedPoints
		else
			warn("player is new")
			PointsValue.Value = 50 
		end
	else
		warn("DSS failed")
	end

end)


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

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

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

game.Players.PlayerRemoving:Connect(function(Player)
	Save(Player)
end)

game:BindToClose(function()
	warn("waiting 3secs before closing")
	for _, p in pairs(game.Players:GetPlayers()) do
		Save(p)	
	end
	task.wait(3)
end)

Now it’s working every time, thank you so much for the help, I really appreciate you sticking around to help me out.

1 Like

No problem! Glad its working now. Check that stuff about game:BindToClose so you can use it in your future projects too.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.