Not repeating the function connected to .Touched

I have already tried to use a debounce for this but it has not worked for me. I need help so that when a playe rtouches an object, the function only goes off once. Heres my code.

local balance = 0
local part = script.Parent
local debounce = false

part.Touched:Connect(function(hit)
        if debounce == false then
                debounce = true
                balance += 1
                part.CanCollide = false
                debounce = false
      end
end)

Remove the denounce = false part near the bottom to fix that, you’re basically just calling the function, checking if denounce is off, turning it on, and then back off so it’ll just run forever.

Add in a wait also like so:

local balance = 0
local part = script.Parent
local debounce = false

part.Touched:Connect(function(hit)
        if debounce == false then
                debounce = true
                balance += 1
                part.CanCollide = false
                wait(1)
                debounce = false
      end
end)

That’ll make it so it can be called more than once, but only after 1 second. AFAIK, OP only wants it to run once.

Thank you so much and this helped a ton but I am respawning the part randomly in a test version very similar to this and it is not allowing me to hit it again.

This is the standard way to run a debounce.

Read on the reference:

Oh i see what you mean, you think the OP only wants the entire function to run once and never run again? but why did he even mention debounce then? Hmmm, lets see how they respond.

But they also said this:

Which makes me believe they want a debounce to work.

The wait will allow you to touch it again after 1 second.