Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago

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

Log in to vote
1
Answered by
Puppynniko 1059 Moderation Voter
2 years ago
Edited 2 years ago

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)
0
Cool, I've used raycastHitbox v4.0 before, that is not my question tho. SharkRayMaster 265 — 2y
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 Puppynniko 1059 — 2y
0
Hmm I see, i will try doing that, i also took using my own raycasts into consideration, thanks! SharkRayMaster 265 — 2y
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 SharkRayMaster 265 — 2y
0
Nevermind, figured it out using your idea of debounce but instead i made it into a table, thank for your help! SharkRayMaster 265 — 2y
Ad

Answer this question