How to use debounce in for loops with part.touched?

  1. What do you want to achieve?
    Repair my debounce

  2. What is the issue? Include screenshots / videos if possible!
    code should work 1 time and then wait 1 second but it works from 3 to 25 times then waits 1 second

  3. What solutions have you tried so far?
    I was looking for somone with the same problem but I haven’t found

local debounce = false

for i,v in pairs(workspace:GetDescendants()) do
	if v:IsA("Part") then
		v.Touched:Connect(function()
			if v.Name=="armor" then
				if debounce then return end
				local can=game.ReplicatedStorage.functions.wall_hit:InvokeServer(v)
				if can==true then
					debounce=true
					v.CanCollide=false
					v.CanTouch=false
					v.CanQuery=false
					print(v.needed.Value)
					game.ReplicatedStorage.events.wall_hit:FireServer(v)
					wait(1)
					debounce=false
					v.CanQuery=true
					v.CanTouch=true
					v.CanCollide=true
				end
			end
		end)
	end
end	

To use debounce in a for loop with Part.Touched, you can create a dictionary to store the debounce status for each part. Here’s an example of how to modify your code to use debounce for each touched part:

local debounceDictionary = {}

for i,v in pairs(workspace:GetDescendants()) do
	if v:IsA("Part") then
		debounceDictionary[v] = false
		v.Touched:Connect(function()
			if v.Name == "armor" then
				if debounceDictionary[v] then return end
				local can = game.ReplicatedStorage.functions.wall_hit:InvokeServer(v)
				if can then
					debounceDictionary[v] = true
					v.CanCollide = false
					v.CanTouch = false
					v.CanQuery = false
					print(v.needed.Value)
					game.ReplicatedStorage.events.wall_hit:FireServer(v)
					wait(1)
					debounceDictionary[v] = false
					v.CanQuery = true
					v.CanTouch = true
					v.CanCollide = true
				end
			end
		end)
	end
end

I’ve used debounce=true before can being invoked, and it works here is the code

local debounce = false

for i,v in pairs(workspace:GetDescendants()) do
	if v:IsA("Part") then
		v.Touched:Connect(function()
			if v.Name=="armor" then
				if debounce then return end
				debounce=true
				local can=game.ReplicatedStorage.functions.wall_hit:InvokeServer(v)
				if can==true then
					v.CanCollide=false
					v.CanTouch=false
					v.CanQuery=false
					print(v.needed.Value)
					game.ReplicatedStorage.events.wall_hit:FireServer(v)
					wait(1)
					v.CanQuery=true
					v.CanTouch=true
					v.CanCollide=true
				end
				debounce=false
			end
		end)
	end
end	

The one and only debounce variable is exposed to all functions. This unintentionally allows all Touched callbacks (the functions connected to a part’s Touched signal) to change that value, causing the debounce to constantly flip and make the functions fire erratically.

Place the debounce within the same scope where the v.Touched connection occurs.

Here’s some cleaned-up code, because nesting if statements too much may cause readability problems:

local repStore = game:GetService("ReplicatedStorage")

for i,v in pairs(workspace:GetDescendants()) do
	if not v:IsA("Part") then
		continue
	end
	
	if not (v.Name == "armor") then -- Moved this here. Reduces unnecessary connections to parts that are not named "armor"
		return
	end
	
	local debounce = false -- This debounce variable will be unique to each Part.
	
	v.Touched:Connect(function()
		if debounce then return end
		
		local can = repStore.functions.wall_hit:InvokeServer(v)
		
		if can then
			debounce = true
			
			v.CanCollide=false
			v.CanTouch=false
			v.CanQuery=false
			
			print(v.needed.Value)
			repStore.events.wall_hit:FireServer(v)
			
			wait(1)
			
			v.CanQuery=true
			v.CanTouch=true
			v.CanCollide=true
			
			debounce = false
		end
	end)
end	

As the reply above said, your using one variable debounce for every part, instead give every part its own debounce like this :



for i,v in pairs(workspace:GetDescendants()) do
	if v:IsA("Part") then
        local debounce = false
		v.Touched:Connect(function()
			if v.Name=="armor" then
				if debounce then return end
				local can=game.ReplicatedStorage.functions.wall_hit:InvokeServer(v)
				if can==true then
					debounce=true
					v.CanCollide=false
					v.CanTouch=false
					v.CanQuery=false
					print(v.needed.Value)
					game.ReplicatedStorage.events.wall_hit:FireServer(v)
					wait(1)
					debounce=false
					v.CanQuery=true
					v.CanTouch=true
					v.CanCollide=true
				end
			end
		end)
	end
end

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