Stripping a players weapon

Hello everyone, I’m trying to strip the players active weapon and I can’t seem to figure out how to do it.

Heres the error I’m getting:

[RunString:3] attempt to call method ‘StripWeapon’ (a nil value)

[LUA]

function StripActiveWeapon( self )
self:ChatPrint(“Begin to strip the weapon”)
self:StripWeapon( self:GetActiveWeapon():GetClass() ) – Line 3
self:ChatPrint(“We did it!”)
end
concommand.Add(“TestingCommands”, StripActiveWeapon )
[/LUA]
Any help would be appreciated

As the function isn’t relating to an object you cannot use self. Use something which relates to the player argument instead, such as pl or ply.

Teddi Orange is correct, you need to associate it with the player so:
[lua]
function StripActiveWeapon( ply )
ply:ChatPrint(“Begin to strip the weapon”)
ply:StripWeapon( ply:GetActiveWeapon():GetClass() )
ply:ChatPrint(“We did it!”)
end
concommand.Add(“TestingCommands”, StripActiveWeapon )
[/lua]

Oh alright thanks for the help guys!

I’m just going to expand upon what Tyzer said and mention you don’t have to use pl / ply to represent the player. You could do

[lua]

function StripActiveWeapon( electricboogaloo ) [/lua]

for the player object. As long as it’s not reserved variable (ie if, then, else and so on) it’s fine. Self in this case refers to an object and thus cannot be used.

I tried using this version and I’m getting the same error still:
attempt to call method ‘StripWeapon’ (a nil value)

Hmmm :confused: Really not sure why it doesn’t work.

You are running that function Server side, right?
You can’t run it client-side.

Yeah, I’m running it server side.

Try adding the full amount of arguments, so

[lua]function StripActiveWeapon( ply, c , a )[/lua]

That’s probably what’s causing the issue.

That shouldn’t even matter. Try running it like this:

[lua]function _R.Player:StripActiveWeapon()
if self:IsValid() then
if self:GetActiveWeapon():IsValid() then
self:StripWeapon(self:GetActiveWeapon():GetClass())
else
print(“Invalid Weapon”)
end
else
print(“Invalid Player”)
end
end
concommand.Add(“TestingStuff”, function( ply )
if ply:IsValid()
ply:StripActiveWeapon()
end
end)[/lua]

edit:

Use the new code I just editted, incase the actual weapon might be invalid.

Still getting the same error for some reason:
attempt to call method ‘StripWeapon’ (a nil value)

Add a print before you actually strip the weapon to get the active weapon and the active weapon class:
[lua]
print(self:GetActiveWeapon())
print(self:GetActiveWeapon():GetClass())
[/lua]

Weapon [17]
weapon_physcannon
attempt to call method ‘StripWeapon’ (a nil value)

[lua]function _R.Player:StripActiveWeapon()
if self:IsValid() then
if self:GetActiveWeapon():IsValid() then
self:StripWeapon(self:GetActiveWeapon():GetClass())
else
print(“Invalid Weapon”)
end
else
print(“Invalid Player”)
end
end
concommand.Add(“TestingStuff”, function( ply )
if ply:IsValid() then
ply:StripActiveWeapon()
end
end)[/lua]

I fired up multiplayer and openscripted this and it had stripped correctly. Not sure why you’re getting the error.

I restarted Garrys Mod and it ended up working. I don’t see how that would affect it but whatever. Thanks for the help man I really appreciate it!