• Lua HUD errors....
    16 replies, posted
I'm getting this error [code] [ERROR] gamemodes/darkrp/gamemode/client/hud.lua:212: attempt to call method 'Clip1' (a nil value) 1. DrawHUD - gamemodes/darkrp/gamemode/client/hud.lua:212 2. unknown - gamemodes/ [/code] whenever a player dies from this code (first line is 212) [code] local mag_left = math.Min(localplayer:GetActiveWeapon():Clip1(), 0) local mag_extra = localplayer:GetAmmoCount(localplayer:GetActiveWeapon():GetPrimaryAmmoType()) local ammo = (math.max(localplayer:GetActiveWeapon():Clip1(), 0)) .. " / " .. mag_extra [/code] so I figured I had to check if player was alive before that happens and I get this error [code] [ERROR] lua/includes/modules/draw.lua:73: bad argument #1 to 'GetTextSize' (string expected, got nil) 1. GetTextSize - [C]:-1 2. SimpleText - lua/includes/modules/draw.lua:73 3. DrawHUD - gamemodes/darkrp/gamemode/client/hud.lua:240 4. unknown - gamemodes/darkrp/gamemode/client/hud.lua:400 [/code] But that only happens with this code in there otherwise it works just fine... [code] if localplayer:Alive(IsValid) then local mag_left = math.Min(localplayer:GetActiveWeapon():Clip1(), 0) local mag_extra = localplayer:GetAmmoCount(localplayer:GetActiveWeapon():GetPrimaryAmmoType()) local ammo = (math.max(localplayer:GetActiveWeapon():Clip1(), 0)) .. " / " .. mag_extra end [/code] I'm still learning lua and this has been giving me the run around if someone could teach me why this is being such a pain I'd really appreciate it.
Why are you editing hud.lua?
lol to be honest because I have yet to learn how to make one into module form, I knew as soon as I posted this you were gunna say don't edit core files. <3 might be my favorite person on the forums at this point. lol
You need to use surface.SetFont("myfont") before getting text size.
Can you explain that to me please? I'm only using the fonts DarkRPHUD2 and HudSelectionText at the moment both of which are defined by darkrp, I don't understand why I'd have to define them again and why its only when I added the code to check if the player is alive, it didn't have any errors except the null value when someone died before I entered it. It's this line of code giving off the font error... [code] local sep = sep or "," [/code] its in this function [code] local function formatNumber(n) if not n then return "" end if n >= 1e14 then return tostring(n) end n = tostring(n) local sep = sep or "," local dp = string.find(n, "%.") or #n+1 for i=dp-4, 1, -3 do n = n:sub(1, i) .. sep .. n:sub(i+1) end return n end [/code] not trying to be argumentative or anything I'm just trying to learn the issue and causes so I know for future reference, I do really appreciate the help you've given.
Before you use surface.GetTextSize(text) on line 240 and 400 you need to set the font as I said in the other post. Assuming we're still talking about the second error in your first post.
yes we are, hence why I'm confused because I never used surface.GetTextSize if I pmed you the file would it help?
There is a tutorial on wiki that shows how to make a hud without editing core files (why would you do that?).
I must have missed that one, also I had already been asked and explained it... if you could help me by explaining why when I insert a check for the player being alive before doing the arithmetic for the ammo it gives me a seemingly unrelated error I'd greatly appreciate it.
I'm pretty sure the "Seemingly unrelated error" is caused because the code execution was no longer halted so the code managed to call another function which created an error somewhere else, but previously since the code execution was halted on the previous error that piece of code was never called to produce an error.
The only issue I see with that is the code wasn't halted until death by that error and the hud automatically updates itself (like before the player dead check the hud would work just fine even with the death error once you respawned everything worked) So if the original error was halting the error after the player alive check wouldn't that error still show before the player was killed?
So I've edited the code some, can anyone see an issue with the code provided? any help and I'd love you long time! [code] local function Dafuq() localplayer = localplayer and IsValid(localplayer) and localplayer or LocalPlayer() if not IsValid(localplayer) then return end local shouldDraw = hook.Call("HUDShouldDraw", GAMEMODE, "DarkRP_HUD") if shouldDraw == false then return else if (LocalPlayer():IsValid()) then local mag_extra = localplayer:GetAmmoCount(localplayer:GetActiveWeapon():GetPrimaryAmmoType()) local ammo = (math.max((localplayer:GetActiveWeapon():Clip1()), 0)) .. " / " .. mag_extra --Ammo draw.SimpleText("Clip Spare", "HudSelectionText", ScrW() - 110, ScrH() - 50, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) draw.SimpleText(ammo, "DarkRPHUD2", ScrW() - 105, ScrH() - 25, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) else return end end end hook.Add( "HUDPaint", "Dafuq", Dafuq ) [/code] error its giving me now... [code][ERROR] gamemodes/darkrp/gamemode/client/hud.lua:205: attempt to call method 'GetPrimaryAmmoType' (a nil value) 1. v - gamemodes/darkrp/gamemode/client/hud.lua:205 2. unknown - lua/includes/modules/hook.lua:82 [/code] I think I may have to put a timer to the function to delay it.... but I haven't had any luck getting Simple.timer to work for me... if someone would show me how it looks for that code I'll test it to see if it works please.
Simple.timer doesnt exist, timer.Simple does. Also try [lua] local mag_extra = localplayer:GetAmmoCount(localplayer:GetActiveWeapon()) [/lua]
Pretty sure the error is from the active wep not being valid.
@Aj yea thats what I meant I was half asleep when I finally was like i need help :( changing that code just gives me this error, [code][ERROR] gamemodes/darkrp/gamemode/client/hud.lua:206: bad argument #1 to 'GetAmmoCount' (number expected, got userdata) [/code] chessnut I had the same thought process since it takes less then a second for the gun to spawn I saw else where where people had some luck with putting just a half second-second time on the function because it let the global constants get defined (such as localplayer ect) if you get what im trying to say
[QUOTE=lilplayer1220;41902085]@Aj yea thats what I meant I was half asleep when I finally was like i need help :( changing that code just gives me this error, [code][ERROR] gamemodes/darkrp/gamemode/client/hud.lua:206: bad argument #1 to 'GetAmmoCount' (number expected, got userdata) [/code] chessnut I had the same thought process since it takes less then a second for the gun to spawn I saw else where where people had some luck with putting just a half second-second time on the function because it let the global constants get defined (such as localplayer ect) if you get what im trying to say[/QUOTE] Instead of using a timer ( if im reading this correctly? ) why not just do [lua] if IsValid(localplayer:GetActiveWeapon()) then [/lua]
[QUOTE=Ilyaaa;41908091]Instead of using a timer ( if im reading this correctly? ) why not just do [lua] if IsValid(localplayer:GetActiveWeapon()) then [/lua][/QUOTE] OMG thank you sooooo much I've been fighting with that forever trying to get it to trigger properly <3 <3
Sorry, you need to Log In to post a reply to this thread.