Attempt to call 'GetWeapons'



--Place weapon on body

--Max Primaries Allowed
local MaxPrimaries = 1
--Max SecondariesAllowed
local MaxSecondaries = 2
--Current amount of primaries equipped
local CurrentPrimaries = 0
--Current amount of secondaries equipped
local CurrentSecondaries = 0
--Primaries that will be counted and drawn
local AllowedPrimaries = {"weapon_mad_ak472", "ls_mad_sniper","weapon_mad_m42", "weapon_mad_mac102", "weapon_mad_mp52", "weapon_mad_pumpshotgun2"};
--Secondaries that will be counted and drawn
local AllowedSecondaries = {"weapon_fiveseven2", "weapon_mad_deagle2", "weapon_mad_glock2", "weapon_taser", "weapon_mad_deagle2", "weapon_mad_p2282"};
--Currently equipped primaries
local EquipedPrimaries = {}
--Currently equipped secondaries
local EquipedSecondaries = {}
--Uncounted items
local NoCount = {"arrest_stick", "door_ram", "gmod_tool", "keys", "lockpick", "med_kit", "pocket", "stun_stick", "unarrest_stick", "weaponchecker", "weapon_physgun", "weapon_physcannon", "gmod_camera"}



if (CLIENT) then
--Local player
local pl = LocalPlayer();
local function CheckTable(targ)
for k, v in pairs (targ) do
return v;
end
end 

--Check Secondaries
local CheckS = {}
--ERROR HAPPENS BELOW
for k, v in pairs (pl:GetWeapons()) do
--ERROR HAPPENS ABOVE
if v:GetClass() == CheckTable(AllowedSecondaries) then
table.insert(CheckS, v:GetClass())
end
end
for k, v in pairs (CheckS) do
if v:GetClass() == CheckTable(EquipedSecondaries) then
table.remove(CheckS, k)
else
CurrentPrimaries = CurrentPrimaries - 1
table.remove(EquipedSecondaries)
table.remove(CheckS, k)
end
end

local CheckP = {}
--Error happens below
for k, v in pairs (pl:GetWeapons()) do
--Error happens above
if v:GetClass() == CheckTable(AllowedPrimaries) then
table.insert(CheckP, v:GetClass())
end
end
for k, v in pairs (CheckP) do
if v:GetClass() == CheckTable(EquipedPrimaries) then
table.remove(CheckP, k)
else
CurrentPrimaries = CurrentPrimares - 1
table.remove(EquipedPrimaries)
table.remove(CheckP, k)
end
end
end

if (SERVER) then
local function AddPrimaries(Player, Weapon)
for k, v in pairs (NoCount) do
if Weapon:GetClass() == v then return true end
end
for k, v in pairs (AllowedPrimaries) do
if Weapon:GetClass() == v && CurrentPrimaries < MaxPrimaries then
CurrentPrimaries = CurrentPrimaries + 1
table.insert(EquipedPrimaries, Weapon:GetClass())
return true;
        end
    end
    Player:PrintMessage(HUD_PRINTCENTER, "You already have a primary weapon.")
    return false;
end
hook.Add("PlayerCanPickupWeapon", "CheckPrimaries", AddPrimaries)

local function ResetDD()
    table.Empty(EquipedPrimaries)
    table.Empty(EquipedSecondaries)
    CurrentPrimaries = 0
    CurrentSecondaries = 0
    end
    hook.Add("PlayerDeath", "ResetOnDeath", ResetDD)
    hook.Add("PlayerDisconnected", "ResetOnDisconnect", ResetDD)
    
end


Essentially what I am doing here is trying to keep track of how many weapons a player has. Limiting them to 1 primary and 2 secondaries. However my problem is this: 35] attempt to call method ‘GetWeapons’(a nil value). I have tried just about everything to fix it and it still doesn’t make stop spitting out this error. Please help.(I have marked the places where the error occurs(line 35)).
-Thank you!

Also I have to have these function on there instances so unless I absolutely have to keep them where they are.

You are not declaring the pl variable in the code

Yes he is…
local pl = LocalPlayer();

I thought you were supposed to put tables in “for k,v in pairs(tablegoeshere) do”

Thank you for the responses but it still hasn’t worked. I changed my code with Jova’s suggestion and I get the same error.

I made more tests and the error has something to do with the LocalPlayer not being able to be called. Could I have possibly set up my script’s file structure wronge?
FileStructure:
Myscript -> Lua -> AutoRun -> Script

You’re calling LocalPlayer() before the player is valid.

Use a function and hook it to InitPostEntity.

Sorry… rate me dab reading much :stuck_out_tongue:

You were actually right, since LocalPlayer() is NULL he isn’t defining the variable to any value.

I have a new problem now, see the hook is called only once but I need to be called at all times. Any suggestions?

hey thats funny i want to do this exact(ish) same thing!

and as for your problem, i would imagine there is a command to repeat whatever you need to.

(by the way, i hate the no-avatar avatar)

do it on GM:Think

How would I call 2 hooks at once?
(Sorry for late response)

The code’s a trainwreck and my eyes hurt, so I didn’t read it.

However, if you’re on about calling hooks, you can call as many hooks as you want at any time.

hook.Call(“Some hook” , GAMEMODE , arg1)
hook.Call(“Some other hook type” , nil , arg1 , arg2)