VERY Confusing Error

Hello. I am working on an obby game, and this script below calls a remote function to make the player’s stage the number of the checkpoint they touched. For some wild reason, this script DOES NOT WORK UNTIL a player dies. Anyone know how to fix it? It’s so confusing, here it is.

for _, v in game.Workspace.Checkpoints:GetChildren() do
	v.Touched:Connect(function(hit)
		local hum = hit.Parent:FindFirstChild("Humanoid")
		local char = hit.Parent
		if hum then
		
			local plr = game.Players:GetPlayerFromCharacter(char)
			local touchedstage = v.Name
			if tonumber(touchedstage) > plr.leaderstats.Stage.Value and hum.Health > 0 then
				
				game.ReplicatedStorage.Events.ChangeStage:FireServer(touchedstage)
				v.BrickColor = BrickColor.new("Lime green")
				
			end
		end
	end)
	end

Where is the LocalScript located? Because of replication delays, you can’t only loop over the instances once. Otherwise, if instances replicate in after the loop finishes, they will never get set up.

First, make sure the LocalScript is in PlayerScripts. That way it only runs once and multiple events won’t be fired. (I assume it’s somewhere like StarterCharacterScripts, so that’s why it fixes itself after the character dies: it runs again and gets the new checkpoints.)

Second, add a connection to capture all of the Checkpoints being added:

local function setUpCheckpoint(checkpoint)
	checkpoint.Touched:Connect(function(hit)
		local hum = hit.Parent:FindFirstChild("Humanoid")
		local char = hit.Parent
		if hum then
			local plr = game.Players:GetPlayerFromCharacter(char)
			local touchedstage = checkpoint.Name
			if tonumber(touchedstage) > plr.leaderstats.Stage.Value and hum.Health > 0 then
				game.ReplicatedStorage.Events.ChangeStage:FireServer(touchedstage)
				checkpoint.BrickColor = BrickColor.new("Lime green")
			end
		end
	end)
end

-- Set up any existing checkpoints
for _, checkpoint in game.Workspace.Checkpoints:GetChildren() do
	setUpCheckpoint(checkpoint)
end

-- Set up any checkpoints added later
game.Workspace.Checkpoints.ChildAdded:Connect(setUpCheckpoint)

Third, I would recommend stopping the touched connection after the checkpoint is activated so it doesn’t spam the server when Touched fires (it fires a lot).

local function setUpCheckpoint(checkpoint)
	local touchedConnection
	touchedConnection = checkpoint.Touched:Connect(function(hit)
		local hum = hit.Parent:FindFirstChild("Humanoid")
		local char = hit.Parent
		if hum then
			local plr = game.Players:GetPlayerFromCharacter(char)
			local touchedstage = checkpoint.Name
			if tonumber(touchedstage) > plr.leaderstats.Stage.Value and hum.Health > 0 then
				game.ReplicatedStorage.Events.ChangeStage:FireServer(touchedstage)
				checkpoint.BrickColor = BrickColor.new("Lime green")
				-- Stop the touched connection after the checkpoint is activated
				touchedConnection:Disconnect()
			end
		end
	end)
end

-- Set up any existing checkpoints
for _, checkpoint in game.Workspace.Checkpoints:GetChildren() do
	setUpCheckpoint(checkpoint)
end

-- Set up any checkpoints added later
game.Workspace.Checkpoints.ChildAdded:Connect(setUpCheckpoint)

yoo it works. Thanks a lot for the script. The connection thing worked and i put it in playerscripts.