Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i stop touched event from firing on the same enemy?

Ok so, I'm making some weapons, and there is some zombies, difference between a zombie and a player is that the zombies have an attribute in their humanoid, well, the weapon makes a hitbox by creating a part infront of the player, and checking for touched event to dmg the zombie.

This works perfectly, yet, the touched event fires alot of times as the zombie keeps touching the hitbox.

Using "debounce" wont work, because I want the weapon to be able to damage many zombies at once, just not the same ones.

So my question is:

How do I check if that zombie has already been damaged by the attack?

Here's my code if you need it:

hitbox.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if hit.Parent.Humanoid:GetAttributes("IsNPC") then
				local npc = hit.Parent
				local hum = npc.Humanoid
				
				hum:TakeDamage(35)
			end
		end
	end)

"hitbox" is the part I use as the hitbox.

1 answer

✓ Accepted answer

First dont use touched event to attack write your own ray cast function or use raycastHitbox v4.0 it handles hit for you can can hit multiple enemy

click here

edit: if you somehow cant use RaycastHitbox then you can try this

local Db = nil
local Hitbox = script.parent.Hitbox
Hitbox.Touched:Connect(function(OtherPart)
	if Db ~= OtherPart.Parent then
		Db = OtherPart.Parent
		wait(1)
		Db = nil
	end
end)

5 comments

0

Cool, I've used raycastHitbox v4.0 before, that is not my question tho.

0

then you can use something like a debounce but instead of bools you use the object value like local db = nil if db ~= Hit.parent then db = Hit.parent wait(1) db = nil end

0

Hmm I see, i will try doing that, i also took using my own raycasts into consideration, thanks!

0

It works, yet, there is a problem, when hitting many zombies at once, it will still spam the Touched event as db = hit.Parent keeps changing from zombie to zombie

0

Nevermind, figured it out using your idea of debounce but instead i made it into a table, thank for your help!

This discussion is preserved as it was. New answers, comments, and votes are closed.