Checking weapon

I completely forgot how to check if a player has a certain weapon out such as weapon_pistol. Can anyone help me out here? Thx in advance. Lolz

**[Player.GetActiveWeapon

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.GetActiveWeapon)**

You still need to get the class of the weapon using GetClass().

So an example of this could be

if LocalPlayer():GetActiveWeapon() == “weapon_pistol” then
print( “Your current weapon is a hl2 pistol.” )
end

??

[lua]
print(LocalPlayer():GetActiveWeapon():GetClass());
[/lua]

if LocalPlayer():GetActiveWeapon():GetClass() == “weapon_pistol” then
print( “Your current weapon is a hl2 pistol.” )
end

:GetActiveWeapon() returns the weapon entity, :GetClass() returns the class name of the entity, and “weapon_pistol” is the classname.

^^ kk ty that helps a lot more than the actual answer. :slight_smile:

[editline]2nd July 2011[/editline]

K so I did this with the given information:

[lua]
function SetKnifeSpeed()
for k,v in pairs( player.GetAll() ) do
if v:GetActiveWeapon():GetClass() == “weapon_ttt_knife” then – line 12
v:SetSpeed( 1 )
end
end
end
hook.Add( “Think”, “SetKnifeSpeed”, SetKnifeSpeed )
[/lua]

and recieved an error saying…
Hook ‘SetKnifeSpeed’ Failed: [@gamemodes errortown\gamemode\speed.lua:12[ Tried to sue a NULL entity!

(Don’t worry about SetSpeed, it works fine with something else I have)

[lua]
function SetKnifeSpeed()
for k,v in pairs( player.GetAll() ) do
if v:GetActiveWeapon() && v:GetActiveWeapon():GetClass() == “weapon_ttt_knife” then – line 12
v:SetSpeed( 1 )
end
end
end
hook.Add( “Think”, “SetKnifeSpeed”, SetKnifeSpeed )
[/lua]

Still getting the error.

[lua]

function SetKnifeSpeed()
for k,v in pairs( player.GetAll() ) do
if IsValid(v) && v:IsPlayer() && v:GetActiveWeapon() && v:GetActiveWeapon():GetClass() == “weapon_ttt_knife” then – line 12
v:SetSpeed( 1 )
end
end
end
hook.Add( “Think”, “SetKnifeSpeed”, SetKnifeSpeed )
[/lua]

Still.

Bump. Anyone know what’s wrong with the above code?



function SetKnifeSpeed()
    for k,v in pairs( player.GetAll() ) do
        if IsValid(v) && v:IsPlayer() && v:GetActiveWeapon():GetClass() == "weapon_ttt_knife" then -- line 12
            v:SetSpeed( 1 )
        end
    end
end
hook.Add( "Think", "SetKnifeSpeed", SetKnifeSpeed )


not sure if this is the problem but I
Removed an extra v:GetActiveWeapon().