How Can I Stop The Touched Event From Firing Multiple Times?

So I was trying to make a health kit that despawns from 10 seconds when you touch it, but since the touched event is happening a lottttt of times per second, not only does it heal the player much more than it should but it’s also grabeable multiple times. I have no idea how to stop it from firing multiple times.
Here’s my code:

available = true

while true do
	if available then
		script.Parent.healpart.Transparency = 0
		script.Parent.healpart.Decal.Transparency = 0
		script.Parent.healpart.Touched:Connect(function(hit)
			if hit.Parent.Humanoid  then
				hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health + 50
				script.Parent.healsound:Play()
				script.Parent.healpart.Transparency = 1
				script.Parent.healpart.Decal.Transparency = 1
				available = false
			end
		end)
	else
		wait(10)
		available = true
	end
	wait(0.1)
end

Thanks a lot for all the help!

4 Likes

Try putting the “available = false” debounce right after “if hit.Parent.Humanoid then” instead of having the debounce change after all the healing stuff occurs.

3 Likes

Additionally, you shouldn’t have a touched event running in a loop, which is most likely the reason it is firing a lot how you described. Just have the touched event on its own as a function, and it will still be fine.

1 Like

After touched make the available false
wait for the cooldown time(depends on how much cooldown time you are looking for) and then make available false

Don’t put the connection in a loop instead just make the connection once at the beginning of the script.

This explains event handling well
Handling Events (roblox.com)

1 Like

Or you could just use the TouchEnded event, which fires after a part detects that another part doesn’t touch it anymore

1 Like

I’ll don’t know what that means, I’ll look into it, but thanks a lot either way! (I’m making this game to learn how to do new things in Roblox Studio so these replies are really useful)

I’ll try that thanks for the feedback again

Thanks, I’ll try this first then the untouched event.

1 Like

I didn’t know this was a thing thanks I’ll try it if the above solution doesn’t work

Actually putting an event inside a loop does not make it fire multiple times but rather creates unnecessary connections which can lead to your game using more resources than required.
This is why it is often suggested to disconnect an event after it is no longer of use(in most cases this is not needed though).

1 Like

Debounce.

available = true

while true do
	if available == true then
		available = false
		script.Parent.healpart.Transparency = 0
		script.Parent.healpart.Decal.Transparency = 0
		script.Parent.healpart.Touched:Connect(function(hit)
			if hit.Parent.Humanoid  then
				hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health + 50
				script.Parent.healsound:Play()
				script.Parent.healpart.Transparency = 1
				script.Parent.healpart.Decal.Transparency = 1
				available = true
			end
		end)
	else
		wait(10)
		available = true
	end
wait()
end

6 Likes

thanks a lot for the replies it works as intended now

1 Like