Attemping To Stop Touched Connect Event From Working

I am really confused by my code, this runs the event 5 times when it is touched, and then will run it again 5 times if touched again. It should just allow for the code to only run 5 times and then stop reciving Touched input.

1 Like

I’m confused, why are you using a for loop for an event?

Just add a counter.

Maybe something like:

local touchCount = 0

local function turn(torValue)
	for x-0,10,1 do
		redPart.Touched:Connect(function()
			if touchCount < 5 then
				touchCount = touchCount + 1
				startSpin(2,1,"-Z-X")
			end
		end)
	end
	
end
turn()

If i understand this correctly you want your events to only run once when triggered?

If you want an event to only run once when triggered you should be disconnecting them. All events return a “ScriptConnection” which has a method called “Disconnect()” that you can use to disconnect your event.

Example:

local Connection = nil

local Part = workspace.Baseplate


Connection = Part.Touched:Connect(function()
	Connection:Disconnect()
end)

Quick edit: I just remembered theres also a “Once” method that you can use to run events only once:

This will run the event once when triggered and disconnect it automatically


local Part = workspace.Baseplate


Part.Touched:Once(function()
	print("Hello, world")
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.