Crowbar OnDrop() Script Not Working!

I tried adding this code to the crowbar.
When you drop the crowbar, if you are a certain rank on the server, it is supposed to give you a drawbar (weapon_ttt_drawbar).


local ply = LocalPlayer()
if CLIENT then
 local donorlevel = ply:GetUserGroup()
end
function SWEP:OnDrop()
 
 print(ply)
 local tiers =  {none=1,
    bronze=2,
    silver=3,
    gold=4,
    ostrich=5}
 
 print(ply)
 
 donorlevel = ""
 print(donorlevel)
 local dlevel = 1
 for i,v in pairs(tiers) do  
  if donorlevel:match(i) then
   dlevel = v
   break
  end
 end 
 print(dlevel)
 if SERVER then
 if dlevel > 2 then
  print(ply)
  ply:Give("weapon_ttt_drawbar")
 end
 end
 --self:Remove(ply)
end

However, I get this error message:

Look at the scope of where ‘ply’ is defined.

Sweps are registered before the Local Player entity exists.
‘ply’ is getting initialized with ‘nil’, which you cannot index.

What you would want to do is store the variable on the weapon itself and set it in an event where the owner has changed, such as: OwnerChanged

OK, I changed the code


function SWEP:OwnerChanged()
 if CLIENT then
  self.localplayer = LocalPlayer()
 end
 print (self.localplayer)
end

function SWEP:OnDrop()
 self:Remove()
end

function SWEP:OnRemove()
 local tiers =  {none=1,
    bronze=2,
    silver=3,
    gold=4,
    ostrich=5}
 
 if CLIENT then
  local donorlevel = self.localplayer:GetUserGroup()
  print(donorlevel)
  self.dlevel = 1
  for i,v in pairs(tiers) do  
   if donorlevel:match(i) then
    self.dlevel = v
    break
   end
  end 
  print(self.dlevel)
 end
 
 if self.dlevel > 2 then
  if SERVER then
   self.localplayer:Give("weapon_ttt_drawbar")
  end
 end
end

but I’m getting a new error

It should be LocalPlayer(). You need the parenthesis and capitals.

Did you read the code?

I’m the guy who’s acutally writing this code, and if you read it, I’m actually setting the sweps localplayer variable (as per the advice of the first reply) to LocalPlayer(). It seems to be throwing an error with the server sometimes, and with the client occasionally, despite the fact that the client should know what LocalPlayer() is, and the server should know since the client has set it inside the weapon

I read the code, you’re calling a clientside variable serverside. And in the clientside portion, you have no validity checks, meaning that if OwnerChanged isn’t called before OnRemove, it will error. Maybe you should actually read my post first before rating it dumb.

From reading the code, you have ‘self.dlevel’ declared on the client side, but you’re trying to access it both clientside and serverside.

[lua]
if CLIENT then
local donorlevel = self.localplayer:GetUserGroup()
print(donorlevel)
self.dlevel = 1
for i,v in pairs(tiers) do
if donorlevel:match(i) then
self.dlevel = v
break
end
end
print(self.dlevel)
end

if self.dlevel > 2 then
if SERVER then
self.localplayer:Give(“weapon_ttt_drawbar”)
end
end
[/lua]

Dude, your post was about LocalPlayer() being spelled wrong (which it wasn’t), and was completely different to what you just told me, so I would consider that to be somewhat dumb. On the other hand, this is not. Is OwnerChanged not called when the player picks up the crowbar? So surely I shouldn’t need to worry about it not being run yet. So how can I then pass the current player to the server, and then give that player a weapon while ensuring the server actually does know what the player is?

Net messages? And I never said it was spelled incorrectly.