Touched event multiple times

hi i was trying to create a script that would print me whenever a block from a group hits the baseplate. It manages to do it, but it prints a single block multiple times and I only want it to print it once for each, how could I fix it? here the code

local debounce = false

Baseplate.Touched:Connect(function(hit)
    local BaTouch=Baseplate:GetTouchingParts()
    hit.Transparency=1
    for i,block in pairs(BaTouch) do
        if block:IsA("Part") and debounce==false then
            debounce = true
            print(block)
            debounce= false
    end    
  end
end)

The fix is very simple, after the print(block), you forgot to put a wait function there.

Also, the formatting of the code is pretty bad, like putting a space the end of most commas, and around math operators, like β€œ+ - = / * > <”, and LuaU operators like β€œ+= -= /= *= <= >=”.

Updated code:

local debounce = false

Baseplate.Touched:Connect(function(hit)
	local BaTouch = Baseplate:GetTouchingParts()
	hit.Transparency = 1
	for i, block in pairs(BaTouch) do
		if block:IsA("Part") and debounce == false then
			debounce = true
			print(block)
			task.wait(1) -- 1 second delay in between each event.
			debounce = false
		end    
	end
end)

If you want for the event to happen only once, just don’t set debounce to false again, or you could disconnect the function after each event like so:

local connection

connection = Baseplate.Touched:Connect(function(hit)
	local BaTouch = Baseplate:GetTouchingParts()
	hit.Transparency = 1
	for i, block in pairs(BaTouch) do
		if block:IsA("Part") then
			print(block)
			connection:Disconnect()
		end    
	end
end)
1 Like