Problem with loops and debounce

Basically i have a Folder and in this folder there are multiple Models. Into these models there are:

  1. parts
  2. an intvalue whose name is the name of a player
    (here is an example)

I want to check if a player touches one of those parts (with specific requirements, for example if there is not the name of the player into the model itself), then there is a fireserver

So i tried to make a loop and added a debounce system, i want that if the player touches the part, then there is the fireserver ONLY 1 time, but i probably did a mistake because when a player touches one of the parts (with all the requirements), then it Fires exactly 2 times.

This is a local script

for _, part in pairs(game.Workspace.BodyParts:GetDescendants()) do		
			if part:IsA("Part") or part:IsA("MeshPart") then
				
				local debounce = false 

				local connection = part.Touched:Connect(function(hit)
					
					if debounce then 
						print("DEB")
						return
					end 

					if hit.Parent:FindFirstChildWhichIsA("Humanoid") and not debounce then
						local player = game.Players:GetPlayerFromCharacter(hit.Parent)
						print("HIT")
						if player and not part.Parent:FindFirstChild(player.Name) then
							 
							part.CanTouch = false 

							print(player.Name.. " TOUCHED ".. part.Name.. " OF "..part.Parent:FindFirstChildWhichIsA("IntValue").Name)
							  
							debounce = true
							 
							TouchBodyPartEvent:FireServer(part.Name)
							
						   task.wait(2)
						   debounce = false
						end
					end
				end)
			end		
		end

Basically it prints "player.Name.. " TOUCHED ".. part.Name.. " OF “..part.Parent:FindFirstChildWhichIsA(“IntValue”).Name” exactly 2 times and it prints “HIT” 2 times and it doesn’t print “DEB” when the player touches

i don’t really understand where should i put the “debounce” and the reason why it doesn’t work, so if you know it may you please also explain why (besides fixing it). Thank you.

You probably want debounce to be a singular values?
Here i also optimized your code a little:

local Players = game:GetService("Players")
local BodyParts:Instance = workspace.BodyParts
local debounce:boolean = false

for _, part in BodyParts:GetDescendants() do	
	if not part:IsA("BasePart") then continue end
	local part_Parent = part.Parent

	part.Touched:Connect(function(hit:BasePart):()
		local hit_Parent = hit.Parent::Instance
		if debounce or not hit_Parent:FindFirstChild("Humanoid") then return end
		local plr = Players:GetPlayerFromCharacter(hit_Parent)::Player?
		if not plr or part_Parent:FindFirstChild(player.Name) then return end
		debounce = true
		part.CanTouch = false
		print(player.Name.. " TOUCHED ".. part.Name.. " OF "..part.Parent:FindFirstChildWhichIsA("IntValue").Name)
		TouchBodyPartEvent:FireServer(part.Name)

		task.wait(2)
		debounce = false					
	end)
end
1 Like

Thank you for your answer
I had already tried to put the debounce outside the loop, but for a reason that i don’t know it is still continuing to ignore the debounce as soon as i touch a part and for some reason it prints “HIT” and the other thing exactly 2 times:

Hav you tried putting the debounce outside of the part.Touched but inside the for loop?
Good luck :+1:

1 Like

Maybe try to not change CanTouch?
Could be racing conditions aswell idk.

1 Like

you want something like this ?

1 Like

Thanks for your answer
Yeah, exactly. I’d like that when someone touches a part it detecs only one time instead of two. Right now i’m trying to shift it to a serverscript and put for each player a nominal debounce. If nothing works, then i’ll put a debounce in the “fireserver” (even if it’s not very efficient)