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

Sword only affects one humanoid and ignores all others that it touched later?

Asked by 3 years ago

So i want my sword to be "multi-target" but the script finds one humanoid and affects that one ignoring all other humanoids. here's the code :

local weapon = script.Parent
local dmg = 20

weapon.Touched:connect(function(part)
    if part.Parent:FindFirstChild("Humanoid")then
        if CanDamage.Value == true then -- i hid where CanDamage came from
            local humanoid = part.Parent:FindFirstChild("Humanoid")
            humanoid:TakeDamage(dmg)
            CanDamage.Value = false -- here too
        end
    end
end)

Thanks in advance

0
Well yes, that's how it's supposed to work. It stops dealing damage the first time it finds a character. You explicitly made it do this on line 09 when you set CanDamage to false. radiant_Light203 1166 — 3y
0
i set it to false to prevent it from damaging the same humanoid more than once, if i removed it would kill the player in just one it XD_Destroyer1234 4 — 3y
0
That's not what it's doing. It isn't specific to a humanoid, what it's doing is stopping the sword from dealing damage at all. radiant_Light203 1166 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

The CanDamage variable, when set to false, stops the part from dealing damage after the first hit. Regardless of how many other humanoids it hits. The solution? Create a table which tracks what humanoids have been hit and stops the function from running if it found them in it.

local humanoidsThatHaveBeenHit = {}

weapon.Touched:connect(function(part)
    if part and part.Parent then
        local humanoid = part.Parent:FindFirstChild("Humanoid")
        if humanoid and not humanoidsThatHaveBeenHit[humanoid] then
            humanoidsThatHaveBeenHit[humanoid] = true
            humanoid:TakeDamage(20)
        end
    end
end)

At the end of the attack; maybe once the animation has stopped running. Or however you've coded it you can simply do humanoidsThatHaveBeenHit = {}.

0
thanks a lot XD_Destroyer1234 4 — 3y
0
Did it work atleast? radiant_Light203 1166 — 3y
0
hmm actually it did, but just once, it wont deal anymore damage after the first time XD_Destroyer1234 4 — 3y
0
yes, it deals damage to all humanoids, but it can also damage even if i do not swing. XD_Destroyer1234 4 — 3y
View all comments (5 more)
0
Do you clear the table once the attack is over? radiant_Light203 1166 — 3y
0
Yes i did that XD_Destroyer1234 4 — 3y
0
That doesn't cover the scope of the question, so I won't answer in much detail. The problem is because this function runs outside of the swinging portion of your script. Connect the function from inside that scope, and remember to disconnect it when it's over. radiant_Light203 1166 — 3y
0
I will try that in a few minutes, im taking a small break so please keep an eye out here XD_Destroyer1234 4 — 3y
0
If you'd prefer to, my Discord tag is @Tom#5712. radiant_Light203 1166 — 3y
Ad

Answer this question