Need help with my .Touched script

What I’m trying to achieve
I’m trying to do a touched event where it doesn’t print countless times

What I tried
I tried putting a while wait(1) loop and it didn’t work, I also tried adding the hit.Name = “RightFoot” but it didn’t work either :confused:

Script

while wait(1) do
	script.Parent.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			print("Player touched part")
			hit.Parent:MoveTo(Vector3.new(0,100,0))
		end
	end)
end

Output
image

Can someone please tell me any solutions or ideas? I need this for a game I’m working on, I would really appreciate it :smiley:

The issue is you have this in a while loop. So every second you are creating a new connection. Events are allowed to have multiple listeners so that works. Take it out of the while. You don’t need it here.

1 Like
local debounce = true
while wait(1) do
	script.Parent.Touched:Connect(function(hit)
    if debounce then
        debounce = false
		if hit.Parent:FindFirstChild("Humanoid") then
			print("Player touched part")
			hit.Parent:MoveTo(Vector3.new(0,100,0))
           wait(1)
           debounce = true
		end
      end
	end)
end
2 Likes

A debounce doesn’t really solve the problem. The problem is there were tons of connections being made every second.

1 Like

Yeah my bad, I didn’t notice the while loop.

Even though many connections are made it worked :eyes:

Consider taking out the while loop though, it isn’t really needed.

Okay, thank you for your advice :smiley: