Checkpoints Green on Touch (LocalScript)

Hi there, scripting’s not my specialty so I’m struggling here.

The result of the code should be as such:
For every stage cleared (indicated by scoreboard counter) parts named in numerical order (I.E checkpoint 1 is called “1”, checkpoint two is “2”) will turn to green, this will only effect client-side

The checkpoints are in a folder under workspace called “Stages”

I’m ashamed to post it since it’s so far from correct, but here’s what I tried

local Checkpoints = game.Workspace.Stages
local Debounce = false

for index, value in pairs(Checkpoints:GetChildren()) do
	if value.Touched:Connect(function(part)
			local partParent = part.Parent
			local player = game.Players:GetPlayerFromCharacter(partParent)
			local leaderstats = player.leaderstats
			local playerStage = leaderstats.Stage
			if tonumber(value.Name) == playerStage.Value + 1 then
				if not Debounce then
					Debounce = true
					print("Working checkpoint!")
					value.BrickColor = BrickColor.new(58, 255, 81)
					wait()
					Debounce = false
					if player and part:IsDescendantOf(game.Players.LocalPlayer.Character) then
						value.BrickColor = BrickColor.new(58, 255, 81)
					end
				end
			end
		end)
	end
end

Thank you!

The loop only runs once, put it in a while loop (add a wait so your studio doesn’t crash)

could I not use reaching the checkpoint as a trigger for the loop? or do I need a while to detect that?

The for loop only runs once, so the touch event only runs once.

(Sleeping, I will reply in the morning)

Ahh, okay

Able to say what else in the code’s wrong? Confident there’s a lot more than just that.

Fun anecdote, I took CS in college for two years… clearly I didn’t pay much attention :stuck_out_tongue:

You have too many ends and you don’t need an if statement when connecting an event to a function