attempt to call method 'PrintMessage' (a nil value)

Hello, I am getting the error “attempt to call method ‘PrintMessage’ (a nil value)” when I am running my script.



if SERVER then

	function WeaponEquip(wpn)

	local foundWeapon = false
	local ply = wpn:GetOwner()
	for i=1,20 do
		if wpn:GetClass() == RestrictedWeapons*.Name then
			foundWeapon = true
			wpn:Remove()
		end
	end
		if foundWeapon == true then
			ply:PrintMessage( HUD_PRINTTALK, "[AMod] Sorry, you need a higher rank to carry this weapon.")
		end
	end
	hook.Add( "WeaponEquip", "PlayerWeaponEquip", WeaponEquip )
 
end


Your help is appreciated, thanks :slight_smile:

You’re not defining what ply is, edit your function WeaponEquip (wpn) with, function WeaponEquip (wpn, ply )

Thanks :slight_smile:

[editline]05:30PM[/editline]

Edit: Changed it, still getting the same error :confused:

-snip-

Your dumb… Also OP, I can’t see what the problem is atm, I’ll just quick test it.

Edit:
OP read the notes here: **[Gamemode.WeaponEquip

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

Or if your lazy: *At the time this hook is called, the weapon doesn’t have an owner. wep:GetOwner will return a Null Entity. The Weapon Owner is set on the next frame. I found it easiest to use timer.Simple to get around this problem. As shown in the example above. *