Touched() activating thousands of times

so the script is supposed to give you one gem and activate a function to move the gem to another location.

while wait() do
for i,v in pairs(gems:GetChildren()) do
	v.Hitbox.Touched:connect(function(hit)
		if debounce == false then 
			debounce = true
			if hit.Parent:FindFirstChild("Humanoid") then
				Collected(v)
				game.Players[hit.Parent.Name].leaderstats.Gems.Value += 1
			end
			debounce = false
		end
	end)
end
end

I tried to make the denounce, but it didn’t work.
https://gyazo.com/aa934b938af7f8bdf41277d0d1f21b92

1 Like

It’s debounce not denounce

try this:

local debounce = false
for i,v in pairs(gems:GetChildren()) do
	v.Hitbox.Touched:connect(function(hit)
		if debounce then
			return
		end
		debounce = true
		if hit.Parent:FindFirstChild("Humanoid") then
			Collected(v)
			game.Players[hit.Parent.Name].leaderstats.Gems.Value += 1
		end
		debounce = false
	end)
end

Do

Wait(time here)
debounce = false

cause debounce is false again immediately so you just made the debounce useless

https://gyazo.com/851843274d6c7e410eaafc8e15226e2e
no longer registers the touch

the same thing still happens unfortunately. I used wait(1)

Your Touched connection is activating thousands of times because you are registering connections every ~0.03 seconds (wait()). What are you trying to achieve?

so another script makes 10 (changed to one for this example) ‘gems’ in random positions.

when a gem is touched, it moved to another position

Oh btw, @dude7271, you should get rid of the while wait() do end
(what GalaxyRIP324 is saying)

I updated my code, there was an extra end

yeah, but now it only loops through the gems one, and isn’t active by the time the character is in the game

Make it a function

function addGem(gem)
	local debounce = false
	gem.Hitbox.Touched:connect(function(hit)
		if debounce then
			return
		end
		debounce = true
		if hit.Parent:FindFirstChild("Humanoid") then
			Collected(gem)
			game.Players[hit.Parent.Name].leaderstats.Gems.Value += 1
		end
		debounce = false
	end)
end

Whenever you create a new gem, call it again.

Where did you define the status of your variable “denounce”? Can you show us your whole script? You’re missing essential info for us to help you out here…

1 Like

Use TouchEnded to make debounce = false

When I was learning about debounces it took me a bit to wrap my head around them but they’re actually very simple.

Basically, you’re only letting something happen when the debounce is false, and so when something happens, you set it to true, so that it can’t happen again. In your script, you’re setting it to true but then almost immediately setting it back to false, letting it happen again. If you only want it to happen once, just delete “debounce = false” at the end, but if you just want a delay, add a wait(time here) right before it to give it some time.

Hope this helped.

local debounce = false
while wait() do
	for i,v in pairs(gems:GetChildren()) do
		v.Hitbox.Touched:connect(function(hit)
			if debounce == true then
			elseif debounce == false then
				debounce = true
				if hit.Parent:FindFirstChild("Humanoid") then
					Collected(v)
					game.Players[hit.Parent.Name].leaderstats.Gems.Value += 1
					
					wait(1) -- Add a cooldown for the debounce

					debounce = false	
				end
			end
		end)
	end
end

hey what is this

gems:GetChildren()

where is the variable for gems?

Instead of using a debounce variable why not use the wait() function?
This is what I would do:

while wait() do
for i,v in pairs(gems:GetChildren()) do
	v.Hitbox.Touched:connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				Collected(v)
				game.Players[hit.Parent.Name].leaderstats.Gems.Value += 1
			end
        script.Parent.Transparency = 1 --Not required but makes it look nicer.
		script.Disabled = true --Don't allow users to use the script
		wait(5)  --Wait X seconds
		script.Parent.Transparency = 0 --Make it seeable again
		script.Disabled = false		--Re-enable the script.
		end
	end)
end
end

I can’t test this as I don’t have the collected var but it should work.

debounce=false
cooldown=2
while wait() do
for i,v in pairs(gems:GetChildren()) do
    v.Hitbox.Touched:connect(function(hit)
        local humanoid=hit.Parent:FindFirstChild("Humanoid")
        if debounce == false then 
            debounce = true
            if humanoid then
                Collected(v)
                game.Players[hit.Parent.Name].leaderstats.Gems.Value += 1
            end
            wait(2)
            debounce = false
        else
            return
        end
    end)
end
end

Why do you need while wait()? Can’t you used touched event without it?

Since for some reason everyone wants to keep the while loop in the code, I’m just gonna mention something that should be kind of obvious.

You. Don’t. Need. The. While. Loop.

A simple script like this…

while wait() do
	script.Parent.Touched:Connect(function()
		print("LAG INBOUND!")
	end)
end

And the massive lag spike that comes just from touching the part should show why this is bad. And guess what, adding a debounce won’t help when you have 600+ touched events connected to your instance just seconds after running your code.

I know this post is kind of getting ranty, but for the past two hours this has just been overlooked. You only need to make an event connection once. And the effects of using this while loop run down into your code. Even with a proper debounce you risk massive performance drops or have the debounce be a global debounce meaning once one player touches any part the debounce is applied to all parts in the folder.

I could not reinforce this enough but get rid of the while loop. Code like shown below is completely functional.

for _,part in ipairs(workspace:WaitForChild("Folder"):GetChildren()) do
	part.Touched:Connect(function()
		local debounce = false
		if not debounce then
			print("YAY!")
			debounce = not debounce
			wait(2)
			debounce = not debounce
		end
	end)
end
1 Like