Issue with leaderstats

Hey! I am having an issue with my leaderstats. I’m not sure if it’s just a studio bug or if it is an issue with the code but sometimes when I playtest my game in studio my “stage” leaderstat doesn’t show up but my deaths do. I also have a data save script that currently isn’t turned on because I don’t have studio API enabled. But sometimes everything works normally and both stage and deaths show up. I don’t get any errors relating to the code for anything so I’m just kinda confused. Thanks!

I should also note that the scripts are still “working” my stages still change my spawn point when I step on them, it just doesn’t show up on my leaderstats sometimes.

Stage Script

-- local checkpoints = workspace:WaitForChild("Checkpoints")

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 = 0
	stage.Parent = leaderstats

	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		wait()
		char:MoveTo(checkpoints[stage.Value].Position)

		hum.Touched:Connect(function(hit)
			if hit.Parent == checkpoints then
				if tonumber(hit.Name) == stage.Value + 1 then
					stage.Value = stage.Value + 1
				end
			end
		end)
	end)
end)

Deaths Script

-- local CountName = "Deaths" 






-------------------------------------------------------------------
local Players = game:GetService('Players')

local PlayerAdded = function(player)
	local leaderstats = player:FindFirstChild('leaderstats') or Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local WOs = leaderstats:FindFirstChild(CountName) or Instance.new("IntValue", leaderstats)
	WOs.Name = CountName
	WOs.Value = 0

	local CharacterAdded = function(character)
		local d = true
		character:WaitForChild("Humanoid").Died:connect(function()
			if (d) then
				d = false
				WOs.Value += 1
			end
		end)
	end

	CharacterAdded(player.Character or player.CharacterAdded:Wait())
	player.CharacterAdded:connect(CharacterAdded)
end

for _, player in next, Players:GetPlayers() do
	spawn(function()
		PlayerAdded(player)
	end)
end
Players.PlayerAdded:Connect(PlayerAdded)

leaderstats

1 Like