How to tell when a player loaded a leaderstats value

I made a mistake, back in the early days of this small little game I type a 0 instead of a 1, that little typo made the progress/whole point of said game unplayable/ you cannot progress. I want to fix that, but I cant, because this script keeps throwing this error up:

leaderstats is not a valid member of Player “Players.Spiyder1”

Here is the script:

game.Players.PlayerAdded:Connect(function(player)
	repeat
		wait()
	until player.leaderstats.Level.Value ~= nil
		if player.leaderstats.Level.Value == 0 then
			player.leaderstats.Level.Value = 1
		end
end)

Error is on line:

if player.leaderstats.Level.Value == 0 then

How would I check if a player has this leaderstats value?

Use :WaitForChild() to wait until the leaderstats are present.

1 Like

You can use :WaitForChild()

local leaderstats = player:WaitForChild("leaderstats")
local Level = leaderstats.Level
-- rest of the code --
2 Likes

try this

game.Players.PlayerAdded:Connect(function(player)
	repeat
		wait()
	until player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Level")
	
	if player.leaderstats.Level.Value == 0 then
		player.leaderstats.Level.Value = 1
	end
end)
1 Like

thanks, I knew I was missing something.