How to make TouchEnded event finish its cycle before Touched event gets fired?

Hello developers! I have recently started to learn how to script, and I have created a couple scripts that use “Touched” and “TouchEnded” events. I found a problem with them. If I touch the Sensor part that is used for activating the events, I can make the “Touched” event fire before the “TouchEnded” finishes. How can I make it so that the “Touched” event does not fire before “TouchEnded” Event completes its cycle? If anyone wants to read the script, ask me. I can send it in the replies.
Sorry for bad English.
God bless you.

1 Like
local part = script.Parent

part.Touched:Connect(function(touched)
	local endedEvent
	endedEvent = part.TouchEnded:Connect(function(ended)
		if touched == ended then
			endedEvent:Disconnect()
			print('fire!')
		end
	end)
end)

Although, you may want to use some type of debounce depending on what you’re doing.

1 Like

So you want the Touched event to fire, run it’s course, then have the TouchEnded fire?
As @MightyDantheman said, use a Debounce.

But do you really need the TouchEnded? What are you using the script for, a door opening, a sound playing while you’re in a certain area, damage to a player? We need to know what it’s for to be able to understand your issue.

1 Like

I made the script for a traffic light that activates when a player gets into the sensor. Here is the script:

Red = game.Workspace.FirstProject.Red
Yellow = game.Workspace.FirstProject.Yellow
Green = game.Workspace.FirstProject.Green
Red2 = game.Workspace.FirstProject["Red 2"]
Yellow2 = game.Workspace.FirstProject["Yellow 2"]
Green2 = game.Workspace.FirstProject["Green 2"]
Sensor = game.Workspace.FirstProject["Sensor 1"]
Sensor2 = game.Workspace.FirstProject["Sensor 2"]

Red.Material = Enum.Material.Neon
Red2.Material = Enum.Material.Neon
Sensor.Touched:Connect(function()
	wait(2)
	Red.Material = Enum.Material.Plastic
	Green.Material = Enum.Material.Neon
end)

Sensor.TouchEnded:Connect(function()
	wait(3.5)
	Green.Material = Enum.Material.Plastic
	Yellow.Material = Enum.Material.Neon
	wait(5)
	Yellow.Material = Enum.Material.Plastic
	Red.Material = Enum.Material.Neon
end)
Sensor2.Touched:Connect(function()
	wait(2)
	Red2.Material = Enum.Material.Plastic
	Green2.Material = Enum.Material.Neon
end)
wait(10)

Sensor2.TouchEnded:Connect(function()
	wait(5)
	Green2.Material = Enum.Material.Plastic
	Yellow2.Material = Enum.Material.Neon
	wait(5)
	Yellow2.Material = Enum.Material.Plastic
	Red2.Material = Enum.Material.Neon
end)

The traffic light detects a player and it stays green while the player is in the sensor. The player leaves the sensor and the light turns yellow and then green.
I am new to scripting so I have no idea how to even use debounce lol

That’s why I included the debounce link in my previous post.
It explains how Touched will fire multiple times whenever a player and their parts contacts the item.

1 Like

The term “debounce” essentially just means cooldown. Here’s an example:

local part = script.Parent
local debounce = false
local cooldown = 5 -- The amount of seconds before you want the touched to be able to be activated again

part.Touched:Connect(function()
	if debounce == false then
		debounce = true
		-- Enter the code you want to run here once the user has Touched the Part
		task.wait(cooldown) -- Waits before being able to run the Touched Event again
	else
		warn("There is a cooldown, please wait "..cooldown.." seconds!") -- Simple message for the output - Just for example purpose
		-- Run code here if there is a debounce
	end
end)

Essentially you disable it and enable it as you go and and a cooldown because if the user is standing on the part, its going to keep running the Touched Event over and over however, by adding a boolean, it sort of adds a restriction to it meaning you’re able to control when its re-enabled with the cooldown of course. I’ll add another example now for one of your Sensors so you get what I mean by this:

Sensor.TouchEnded:Connect(function()
	if debounce == false then
		debounce = true
		wait(3.5)
		Green.Material = Enum.Material.Plastic
		Yellow.Material = Enum.Material.Neon
		wait(5)
		Yellow.Material = Enum.Material.Plastic
		Red.Material = Enum.Material.Neon
                wait(cooldown)
                debounce = false
	else
		print("Please wait for the cooldown to finish")
		-- Run code if there is a cooldown/debounce
	end
end)

I highly recommend putting these scripts in the different sensor parts and shortening them in there as the debounce can control every single debounce in the script, that is unless you are willing to have extra variables such as SensorDebounce, Sensor2Debounce etc… (will provide another example - if you are using the same script)

local Sensor = game.Workspace.FirstProject["Sensor 1"]
local Sensor2 = game.Workspace.FirstProject["Sensor 2"]
local SensorDebounce = false
local Sensor2Debounce = false
local cooldown = 5 -- The amount of seconds before you want the touched to be able to be activated again

Sensor.TouchEnded:Connect(function()
	if SensorDebounce == false then
		SensorDebounce = true
		wait(3.5)
		Green.Material = Enum.Material.Plastic
		Yellow.Material = Enum.Material.Neon
		wait(5)
		Yellow.Material = Enum.Material.Plastic
		Red.Material = Enum.Material.Neon
	else
		print("Please wait for the cooldown to finish")
		-- Run code if there is a cooldown/SensorDebounce (same thing)
	end
end)

Sensor2.Touched:Connect(function()
	if Sensor2Debounce == false then
		Sensor2Debounce = true
		wait(2)
		Red2.Material = Enum.Material.Plastic
		Green2.Material = Enum.Material.Neon
		wait(cooldown)
		Sensor2Debounce = false
	else
		print("Please wait for the cooldown to finish")
		-- Run code if there is a cooldown/SensorDebounce (same thing)
	end
end)

Hopefully you get the point and this helps! Contact me if there is questions/confusion.

1 Like