• [HUD] GetActiveWeapon throwing nil
    6 replies, posted
So, I'm making a HUD. The problem is that once a player joins in, the hud throws an error and doesn't draw. The error: addons/solidhud/lua/autorun/client/solidhudbase.lua:106: attempt to call method 'GetActiveWeapon' (a nil value) It's reffering to this bit of code: local wep = Ply:GetActiveWeapon() if not IsValid(wep) then return end local name = wep:GetPrintName() For some reason the code system here doesn't let me place them correctly, but after end it's a new local variable obviously. I thought it's because the player isn't fully loaded in, so I tried this: if IsValid(LocalPlayer()) then local wep = Ply:GetActiveWeapon() end but that didn't help either. I'm honestly lost at this point.
Where is 'Ply' being defined?
Can you post that section of code instead of snipets? Cause it seems like you need to check if both LocalPlayer() and GetActiveWeapon are valid before you continue, but it only appears that you verify LocalPlayer() is valid.
Same question as beaner. The GetActiveWeapon function doesn't exist on the "Ply" variable when you call it, so I'd suggest looking at/posting the part of code where "Ply" is defined. You can confirm this by calling this instead LocalPlayer():GetActiveWeapon() If this works, then you know for sure something is wrong where you set your "Ply" variable
Ply is defined at the top with my other variables. local Ply = LocalPlayer() Then I have my HUDPaint: hook.Add("HUDPaint", "SolidHUD", function() SOLIDAMMO() SOLIDHUD() end) And then later I do my SOLIDAMMO() function as so: function SOLIDAMMO() local wep = Ply:GetActiveWeapon() if not IsValid(wep) then return end local name = wep:GetPrintName() if name then draw.RoundedBox(20,ScrW()-350,ScrH()-95,325,70,solid_black) CreateImageIcon(SOLIDHUDAmmo,ScrW()-330,ScrH()-80,solid_white)     draw.SimpleText(name,"SolidHUDFont",ScrW()-280,ScrH()-75,solid_white,0,0) end if (Ply:GetActiveWeapon():Clip1()) ~= -1 then draw.SimpleText(Ply:GetActiveWeapon():Clip1().."/"..Ply:GetAmmoCount(Ply:GetActiveWeapon():GetPrimaryAmmoType()),"SolidHUDFont", ScrW()-120,ScrH()-75,solid_white,0,0) else draw.SimpleText(Ply:GetAmmoCount(Ply:GetActiveWeapon():GetPrimaryAmmoType()),"SolidHUDFont",ScrW()-120,ScrH()-75,solid_white,0,0) end end So Ply is defined as LocalPlayer() I can also mention that when you go in the server at first it doesn't work, but then if I go to my file whilst being already in the server and pressing Ctrl+S (save) then it works just fine as it should. It's just the initial part.
If you're defining Ply in the main chunk then it won't ever be valid. You'd need to define it in the hook or in an InitPostEntity hook. LocalPlayer returns NULL before InitPostEntity.
Okay, so I added: hook.Add( "InitPostEntity", "some_unique_name", function() Ply = LocalPlayer() end ) At the top of my variables and now it seems to work just fine! Thank you guys for helping! I'm new to this stuff and this just helped me understand hooks a bit better.
Sorry, you need to Log In to post a reply to this thread.