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

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

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

4 comments

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.

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

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.

1 answer

✓ Accepted answer

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 = {}.

9 comments

0

thanks a lot

0

Did it work atleast?

0

hmm actually it did, but just once, it wont deal anymore damage after the first time

0

yes, it deals damage to all humanoids, but it can also damage even if i do not swing.

0

Do you clear the table once the attack is over?

0

Yes i did that

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.

0

I will try that in a few minutes, im taking a small break so please keep an eye out here

0

If you'd prefer to, my Discord tag is @Tom#5712.

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