Script Breaks If It Returns False

Hello, I have a script in a part that detects when the train touches it. But if it isn’t the train, for example, a player, it doesn’t work again when the train touches it. Here is my script. :arrow_down:

local part = script.Parent
local debounce = false

part.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
		if hit.Parent.Parent.Name == "Locomotive" then
			game.ReplicatedStorage.StartStopTrain:Fire()
			wait(15)
			debounce = false
		end
	end
end)

debounce = true after the wait.

local part = script.Parent
local debounce = false

part.Touched:Connect(function(hit)
	if debounce == false then
		if hit.Parent.Parent.Name == "Locomotive" then
		    debounce = true
			game.ReplicatedStorage.StartStopTrain:Fire()
			wait(15)
			debounce = false
		end
	end
end)
1 Like

I feel kind of stupid, I didn’t see my obvious mistake. :laughing:

I know others gave you your solution, but I’ll show anotehr way you could do what is needed as well!

local part = script.Parent
local debounce = false

part.Touched:Connect(function(hit)
	if debounce or hit.Parent.Parent.Name ~= "Locomotive" then return end
	debounce = true
	game.ReplicatedStorage.StartStopTrain:Fire()
	wait(15)
	debounce = false
end)