Is there a way to make a touched event to happen only once?

I want the leader stats value to only change once, not every time a different body part touches another part. I have tried disabling being able to walk and jump after touching the part but that still didn’t work.

Stage1Check.Touched:Connect(function(hit)
		local humanoid= hit.Parent:FindFirstChild("Humanoid")
		
        if humanoid ~=nil then
            humanoid.WalkSpeed = 0
			humanoid.JumpHeight = 0
			player.leaderstats["Stage 1 Completes"].Value = player.leaderstats["Stage 1 Completes"].Value + 1

			print("I am being Touched")
	     
   end -- then statement end
	end)

You can store the event in a variable and then use :Disconnect() on it

local event =Stage1Check.Touched:Connect(function(hit)
		local humanoid= hit.Parent:FindFirstChild("Humanoid")
		
    if humanoid ~=nil then
        
		player.leaderstats["Stage 1 Completes"].Value = player.leaderstats["Stage 1 Completes"].Value + 1

	   print("I am being Touched")
         event:Disconnect()
	     
    end -- then statement
end)

That doesn’t work.

In the output it says “ServerScriptService.Leader:65: attempt to index nil with ‘Disconnect’”

and the script analysis says " W001: (65,4) Unknown global ‘event’ "

local debounce = false
Stage1Check.Touched:Connect(function(hit)
if not debounce then
debounce = true
		local humanoid= hit.Parent:FindFirstChild("Humanoid")
		
        if humanoid ~=nil then
            humanoid.WalkSpeed = 0
			humanoid.JumpHeight = 0
			player.leaderstats["Stage 1 Completes"].Value = player.leaderstats["Stage 1 Completes"].Value + 1

			print("I am being Touched")
	     
   end -- then statement end
	end)
5 Likes

sorry, it was supposed to be like this

local event 

event=Stage1Check.Touched:Connect(function(hit)
		local humanoid= hit.Parent:FindFirstChild("Humanoid")
		
    if humanoid ~=nil then
        
		player.leaderstats["Stage 1 Completes"].Value = player.leaderstats["Stage 1 Completes"].Value + 1

	   print("I am being Touched")
         event:Disconnect()
	     
    end 
end)

Not an Ideal solution since the touched event won’t be fired every again after its disconnected.

That doesn’t work at all. It just continues running.

Turn the “CanTouch” property of the part the script is housed in, off.

The thing is. Touched event fires only once. It appears to fire multiple times because our characters keeps touching the part. But in reality it fires once for touching the part once.

The only work around I can think of is disconnecting the event, but that will never fire the event again if the brick is touched.

You can also try setting the “CanTouch” property of the part to false after it is fired.

Or use TouchEnded event to check when we stop touching the part.

2 Likes

It worked! but it doesn’t run again after the person stops touching the part. Is there anyway to go around this?

Yea, I understand. I used TouchEnded event and it did the same thing. Its okay, because I found a solution. Thanks for putting in your input

I fixed this issue. I made debounce = false after 5 seconds. Can you explain how you came up with your solution?