The Touched Event

Hey Developers! I have a question. It’s not for a specific script it’s just about the event in general.

For some reason, every time I use that function, it’s for when a player touches a brick. However, apparently, the event gets called about five or six times - the record being twenty-six - when a player touches a brick once. I want it so something happens once, within a time period (probably one or two seconds). I’ve tried a bunch of things to get around this but none of them seem to work.

What are your thoughts on this? What should I do the next time I have this situation? Thanks to whoever replies.

1 Like

Typically called a “debounce” you need to ensure that the brick is called once by adding a delay to how often it can be called.

If you are trying to call it once, you simply do:

local Debounce = false
part.Touched:connect(function()
if p.Parent:FindFirstChild(“Humanoid”) and Debounce == false then
Debounce = true
– code
wait(any time you want before the next call)
Debounce = false
end
end)

4 Likes

Oh. I wasn’t aware that there was a thing called debounce. I learned something new today! Thanks for replying.

Touched fires when any part touches it, not just when the player touches it. If for example the part is touching the baseplate Touched would fire

1 Like

Well, I did put the whole this thing in:

part.Touched:Connect(function(p)
  local humanoid = p.Parent:FindFirstChildWhichIsA("Humanoid")
		if humanoid then
--blah blah blah
        end
end)

So I’m pretty sure that isn’t the problem.

2 Likes

You could use TouchEnded event

1 Like