• Custom Gamemode Error - attempt to call method 'GetActiveWeapon' (a nil value)
    3 replies, posted
Hello, I am currently making a custom gamemode to practice my lua skills. I've encountered an error when creating the HUD for my ammo. Could I have some help please? Here is the error: [CODE][ERROR] gamemodes/deathmatch/gamemode/cl_hud.lua:93: attempt to call method 'GetActiveWeapon' (a nil value) 1. unknown - gamemodes/deathmatch/gamemode/cl_hud.lua:93 2. include - [C]:-1 3. unknown - gamemodes/deathmatch/gamemode/cl_init.lua:2 [/CODE] Here is the relevant code: [CODE]if(client:GetActiveWeapon():IsValid()) then if (client:GetActiveWeapon():GetPrintName() != nil) then draw.SimpleText(client:GetActiveWeapon():GetPrintName(),"Trebuchet24",260,ScrH() - 60,Color(255,255,255), 0, 0) end if (client:GetActiveWeapon():Clip1() != -1) then draw.SimpleText(client:GetActiveWeapon():Clip1().."/"..client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()),"Ammo", 260, ScrH() - 40,Color(255,255,255), 0,0) else draw.SimpleText(client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()),"Ammo",260,ScrH() - 40,Color(255,255,255),0,0) end end[/CODE] Just in case its important, client has been declared as LocalPlayer(). Any help would be appreciated. Thank you.
Use [lua]if(IsValid(client) and IsValid(client:GetActiveWeapon())) then[/lua]
[QUOTE=meharryp;51055379]Use [lua]if(IsValid(client) and IsValid(client:GetActiveWeapon())) then[/lua][/QUOTE] IsValid does what it says, checks if it's valid, but in what case wouldn't the client be valid to the function?
Since client is set to LocalPlayer() then the answer is during the first few frames after the rendering system has been initialized but before the player object has been fully created.
Sorry, you need to Log In to post a reply to this thread.