GetActiveWeapon bug

So, I’m using that


if attacker:GetActiveWeapon() != "weapon_sledgehammer" then print(attacker:GetActiveWeapon()) return end

in a ENT:OnTakeDamage function.
the value “attacker” is


local attacker = damage:GetAttacker()

The console is printing “weapon_sledgehammer” so, I don’t understand why it’s not working.

GetActiveWeapon returns an entity. You need to get its class and check that. if attacker:GetActiveWeapon():GetClass() == “weapon_sledgehammer” then

GetActiveWeapon() returns the weapon itself. If you want to compare with a string, you need to use GetClass(), like so:



if attacker:GetActiveWeapon():GetClass() != "weapon_sledgehammer" then


[editline]inb4[/editline] Beat me to it!

Thank you :stuck_out_tongue:

[editline]13th March 2015[/editline]

So, I have put this:


if not attacker:GetActiveWeapon():GetClass() != "weapon_sledgehammer" then print(attacker:GetActiveWeapon():GetClass()) return end

And it’s printing the good class but it’s not working.

No idea what you mean by its “not working” without seeing the rest of the code, can only assume it’s because you have ‘return’ in there so it is stopping right there.

There’s a word for that! Get it right, sheesh! :eng101:

It’s not working because it’s always “return” even if I’m holding the good weapon.

I’m not half cool enough for ninja’d or inb4! :tinfoil:

That’s because you’re using not before attacker:GetActiveWeapon():GetClass(). The first part evaluates to false and hence you’re always comparing false to a string. It should be this:



if attacker:GetActiveWeapon():GetClass() != "weapon_sledgehammer" then print(attacker:GetActiveWeapon():GetClass()) return end


Oh yes… I didn’t see this…

[editline]14th March 2015[/editline]

THX it work!