HUD won't start when I make a new game.

I posted my hud on the workshop [Only I can see it] I enable it and go into DarkRP single player and I am left with the default Sandbox HUD

The code if you need it :slight_smile:



surface.CreateFont( "playerinfo",  {
    font = "Arial",
    size = 26,
    weight = 1000,
    blursize = 0,
    scanlines = 0,
    antialias = true,
    underline = false,
    italic = false,
    strikeout = false,
    symbol = false,
    rotary = false,
    shadow = false,
    additive = false,
    outline = false,

} )


surface.CreateFont( "hpfont",  {
    font = "Arial",
    size = 16,
    weight = 1000,
    blursize = 0,
    scanlines = 0,
    antialias = true,
    underline = false,
    italic = false,
    strikeout = false,
    symbol = false,
    rotary = false,
    shadow = false,
    additive = false,
    outline = false,

} )

surface.CreateFont( "arfont",  {
    font = "Arial",
    size = 14,
    weight = 2000,
    blursize = 0,
    scanlines = 0,
    antialias = true,
    underline = false,
    italic = false,
    strikeout = false,
    symbol = false,
    rotary = false,
    shadow = false,
    additive = false,
    outline = false,

} )


local function Base()

    local DisplayJob = LocalPlayer():getDarkRPVar( "job" ) or "Citizen"
    local DisplayName = LocalPlayer():Nick() or LocalPlayer():Nick()
    local ourMat = Material( "vgui/saber.png")
    local blue = Material( "vgui/blue.png")
    local red = Material( "vgui/red.png")
    local health = LocalPlayer():Health() or 0
    local armor = LocalPlayer():Armor() or 0

    if health > 100 then health = 100 end
        if health < 0 then health = 0 end
 
        if health != 0 then

        surface.SetDrawColor( 255, 255, 255, 255 )
        surface.SetMaterial( red )
        surface.DrawTexturedRect( ScrW() / 10.5, ScrH () / 1.269, health * 3.005, 47)
    end

    surface.SetDrawColor( 255, 255, 255, 255 )
    surface.SetMaterial( ourMat )
    surface.DrawTexturedRect( ScrW() / 100, ScrH () / 1.246, 140, 30)

    draw.RoundedBox(8, ScrW() / 38.8, ScrH () / 1.24, 60, 30, Color(0, 0, 0, 255))


if armor > 100 then armor = 100 end
        if armor < 0 then armor = 0 end
 
        if armor != 0 then

    surface.SetDrawColor( 255, 255, 255, 255 )
    surface.SetMaterial( blue )
    surface.DrawTexturedRect( ScrW() / 10.5, ScrH () / 1.133, armor * 3, 29)
    
    end
    
    surface.SetDrawColor( 255, 255, 255, 255 )
    surface.SetMaterial( ourMat )
    surface.DrawTexturedRect( ScrW() / 100, ScrH () / 1.128, 140, 30)

    draw.RoundedBox(8, ScrW() / 38.8, ScrH () / 1.124, 60, 30, Color(0, 0, 0, 255)) -- Thanks Dancore for telling me how to make the HUD the same size for all monitors <3


    draw.RoundedBox(8, ScrW() / 70, ScrH () / 95, 300, 60, Color(54, 54, 54, 170))

    draw.SimpleText(health.." HP","hpfont",ScrW() / 32,ScrH() / 1.226,Color(255,255,255,255))
    draw.SimpleText(armor.." %","hpfont",ScrW() / 28,ScrH() / 1.11,Color(255,255,255,255))
    draw.SimpleText( "Name: "..DisplayName, "playerinfo", ScrW() / 60, ScrH() / 95, Color(255,255,255,150))
    draw.SimpleText("Role: "..DisplayJob, "playerinfo", ScrW() / 60, ScrH() / 16, Color(255,255,255,150))

    
end

/*---------------------------------------------------------------------------
HUD ConVars
---------------------------------------------------------------------------*/
local ConVars = {}
local HUDWidth
local HUDHeight
 
local Color = Color
local cvars = cvars
local DarkRP = DarkRP
local CurTime = CurTime
local draw = draw
local GetConVar = GetConVar
local IsValid = IsValid
local Lerp = Lerp
local localplayer
local math = math
local pairs = pairs
local ScrW, ScrH = ScrW, ScrH
local SortedPairs = SortedPairs
local string = string
local surface = surface
local table = table
local timer = timer
local tostring = tostring
 
CreateClientConVar("weaponhud", 0, true, false)
 
local colors = {}
colors.black = Color(0, 0, 0, 255)
colors.blue = Color(0, 0, 255, 255)
colors.brightred = Color(200, 30, 30, 255)
colors.darkred = Color(0, 0, 70, 100)
colors.darkblack = Color(0, 0, 0, 200)
colors.gray1 = Color(0, 0, 0, 155)
colors.gray2 = Color(51, 58, 51,100)
colors.red = Color(255, 0, 0, 255)
colors.white = Color(255, 255, 255, 255)
colors.white1 = Color(255, 255, 255, 200)
 
local function ReloadConVars()
        ConVars = {
                background = {0,0,0,100},
                Healthbackground = {0,0,0,200},
                Healthforeground = {140,0,0,180},
                HealthText = {255,255,255,200},
                Job1 = {0,0,150,200},
                Job2 = {0,0,0,255},
                salary1 = {0,150,0,200},
                salary2 = {0,0,0,255}
        }
 
        for name, Colour in pairs(ConVars) do
                ConVars[name] = {}
                for num, rgb in SortedPairs(Colour) do
                        local CVar = GetConVar(name..num) or CreateClientConVar(name..num, rgb, true, false)
                        table.insert(ConVars[name], CVar:GetInt())
 
                        if not cvars.GetConVarCallbacks(name..num, false) then
                                cvars.AddChangeCallback(name..num, function() timer.Simple(0,ReloadConVars) end)
                        end
                end
                ConVars[name] = Color(unpack(ConVars[name]))
        end
 
 
        HUDWidth = (GetConVar("HudW") or  CreateClientConVar("HudW", 240, true, false)):GetInt()
        HUDHeight = (GetConVar("HudH") or CreateClientConVar("HudH", 115, true, false)):GetInt()
 
        if not cvars.GetConVarCallbacks("HudW", false) and not cvars.GetConVarCallbacks("HudH", false) then
                cvars.AddChangeCallback("HudW", function() timer.Simple(0,ReloadConVars) end)
                cvars.AddChangeCallback("HudH", function() timer.Simple(0,ReloadConVars) end)
        end
end
ReloadConVars()
 
local Scrw, Scrh, RelativeX, RelativeY
/*---------------------------------------------------------------------------
HUD Seperate Elements
---------------------------------------------------------------------------*/
 
local Page = Material("icon16/page_white_text.png")
local function GunLicense()
        if localplayer:getDarkRPVar("HasGunlicense") then
                surface.SetMaterial(Page)
                surface.SetDrawColor(255, 255, 255, 255)
                surface.DrawTexturedRect(RelativeX + HUDWidth, ScrH() - 34, 32, 32)
        end
end
 
local function Agenda()
        local ply = LocalPlayer()
 
        local agenda = ply:getAgendaTable()
        if not agenda then return end
 
        draw.RoundedBox(10, 10, 10, 460, 110, colors.gray1)
        draw.RoundedBox(10, 12, 12, 456, 106, colors.gray2)
        draw.RoundedBox(10, 12, 12, 456, 20, colors.darkred)
 
        draw.DrawNonParsedText(agenda.Title, "DarkRPHUD1", 30, 12, colors.red, 0)
 
        local text = ply:getDarkRPVar("agenda") or ""
 
        text = text:gsub("//", "
"):gsub("\
", "
")
        text = DarkRP.textWrap(text, "DarkRPHUD1", 440)
        draw.DrawNonParsedText(text, "DarkRPHUD1", 30, 35, colors.white, 0)
end
 
local VoiceChatTexture = surface.GetTextureID("voice/icntlk_pl")
local function DrawVoiceChat()
        if localplayer.DRPIsTalking then
                local chbxX, chboxY = chat.GetChatBoxPos()
 
                local Rotating = math.sin(CurTime()*3)
                local backwards = 0
                if Rotating < 0 then
                        Rotating = 1-(1+Rotating)
                        backwards = 180
                end
                surface.SetTexture(VoiceChatTexture)
                surface.SetDrawColor(ConVars.Healthforeground)
                surface.DrawTexturedRectRotated(ScrW() - 100, chboxY, Rotating*96, 96, backwards)
        end
end
 
CreateConVar("DarkRP_LockDown", 0, {FCVAR_REPLICATED, FCVAR_SERVER_CAN_EXECUTE})
local function LockDown()
        local chbxX, chboxY = chat.GetChatBoxPos()
        if util.tobool(GetConVarNumber("DarkRP_LockDown")) then
                local cin = (math.sin(CurTime()) + 1) / 2
                local chatBoxSize = math.floor(ScrH() / 4)
                draw.DrawNonParsedText(DarkRP.getPhrase("lockdown_started"), "ScoreboardSubtitle", chbxX, chboxY + chatBoxSize, Color(cin * 255, 0, 255 - (cin * 255), 255), TEXT_ALIGN_LEFT)
        end
end
 
local Arrested = function() end
 
usermessage.Hook("GotArrested", function(msg)
        local StartArrested = CurTime()
        local ArrestedUntil = msg:ReadFloat()
 
        Arrested = function()
                if CurTime() - StartArrested <= ArrestedUntil and localplayer:getDarkRPVar("Arrested") then
                draw.DrawNonParsedText(DarkRP.getPhrase("youre_arrested", math.ceil(ArrestedUntil - (CurTime() - StartArrested))), "DarkRPHUD1", ScrW()/2, ScrH() - ScrH()/12, colors.white, 1)
                elseif not localplayer:getDarkRPVar("Arrested") then
                        Arrested = function() end
                end
        end
end)
 
local AdminTell = function() end
 
usermessage.Hook("AdminTell", function(msg)
        timer.Destroy("DarkRP_AdminTell")
        local Message = msg:ReadString()
 
        AdminTell = function()
                draw.RoundedBox(4, 10, 10, ScrW() - 20, 100, colors.darkblack)
                draw.DrawNonParsedText(DarkRP.getPhrase("listen_up"), "GModToolName", ScrW() / 2 + 10, 10, colors.white, 1)
                draw.DrawNonParsedText(Message, "ChatFont", ScrW() / 2 + 10, 80, colors.brightred, 1)
        end
 
        timer.Create("DarkRP_AdminTell", 10, 1, function()
                AdminTell = function() end
        end)
end)
 
/*---------------------------------------------------------------------------
Entity HUDPaint things
---------------------------------------------------------------------------*/
local function DrawPlayerInfo(ply)
        local pos = ply:EyePos()
 
        pos.z = pos.z + 10 -- The position we want is a bit above the position of the eyes
        pos = pos:ToScreen()
        pos.y = pos.y - 50 -- Move the text up a few pixels to compensate for the height of the text
 
        if GAMEMODE.Config.showname and not ply:getDarkRPVar("wanted") then
                draw.DrawNonParsedText(ply:Nick(), "DarkRPHUD2", pos.x + 1, pos.y + 1, colors.black, 1)
                draw.DrawNonParsedText(ply:Nick(), "DarkRPHUD2", pos.x, pos.y, team.GetColor(ply:Team()), 1)
        end
 
        if GAMEMODE.Config.showhealth and not ply:getDarkRPVar("wanted") then
                draw.DrawNonParsedText(DarkRP.getPhrase("health", ply:Health()), "DarkRPHUD2", pos.x + 1, pos.y + 21, colors.black, 1)
                draw.DrawNonParsedText(DarkRP.getPhrase("health", ply:Health()), "DarkRPHUD2", pos.x, pos.y + 20, colors.white1, 1)
        end
 
        if GAMEMODE.Config.showjob then
                local teamname = team.GetName(ply:Team())
                draw.DrawNonParsedText(ply:getDarkRPVar("job") or teamname, "DarkRPHUD2", pos.x + 1, pos.y + 41, colors.black, 1)
                draw.DrawNonParsedText(ply:getDarkRPVar("job") or teamname, "DarkRPHUD2", pos.x, pos.y + 40, colors.white1, 1)
        end
 
        if ply:getDarkRPVar("HasGunlicense") then
                surface.SetMaterial(Page)
                surface.SetDrawColor(255,255,255,255)
                surface.DrawTexturedRect(pos.x-16, pos.y + 60, 32, 32)
        end
end
 
local function DrawWantedInfo(ply)
        if not ply:Alive() then return end
 
        local pos = ply:EyePos()
        if not pos:isInSight({localplayer, ply}) then return end
 
        pos.z = pos.z + 14
        pos = pos:ToScreen()
 
        if GAMEMODE.Config.showname then
                draw.DrawNonParsedText(ply:Nick(), "DarkRPHUD2", pos.x + 1, pos.y + 1, colors.black, 1)
                draw.DrawNonParsedText(ply:Nick(), "DarkRPHUD2", pos.x, pos.y, team.GetColor(ply:Team()), 1)
        end
 
        local wantedText = DarkRP.getPhrase("wanted", tostring(ply:getDarkRPVar("wantedReason")))
 
        draw.DrawNonParsedText(wantedText, "DarkRPHUD2", pos.x, pos.y - 40, colors.white1, 1)
        draw.DrawNonParsedText(wantedText, "DarkRPHUD2", pos.x + 1, pos.y - 41, colors.red, 1)
end
 
/*---------------------------------------------------------------------------
The Entity display: draw HUD information about entities
---------------------------------------------------------------------------*/
local function DrawEntityDisplay()
 
        local shootPos = localplayer:GetShootPos()
        local aimVec = localplayer:GetAimVector()
 
        for k, ply in pairs(players or player.GetAll()) do
                if not ply:Alive() or ply == localplayer then continue end
                local hisPos = ply:GetShootPos()
                if ply:getDarkRPVar("wanted") then DrawWantedInfo(ply) end
 
                if GAMEMODE.Config.globalshow then
                        DrawPlayerInfo(ply)
                -- Draw when you're (almost) looking at him
                elseif not GAMEMODE.Config.globalshow and hisPos:DistToSqr(shootPos) < 160000 then
                        local pos = hisPos - shootPos
                        local unitPos = pos:GetNormalized()
                        if unitPos:Dot(aimVec) > 0.95 then
                                local trace = util.QuickTrace(shootPos, pos, localplayer)
                                if trace.Hit and trace.Entity ~= ply then return end
                                DrawPlayerInfo(ply)
                        end
                end
        end
 
        local tr = localplayer:GetEyeTrace()
 
        if IsValid(tr.Entity) and tr.Entity:isKeysOwnable() and tr.Entity:GetPos():Distance(localplayer:GetPos()) < 200 then
                tr.Entity:drawOwnableInfo()
        end
end
 
/*---------------------------------------------------------------------------
Drawing death notices
---------------------------------------------------------------------------*/
function GAMEMODE:DrawDeathNotice(x, y)
        if not GAMEMODE.Config.showdeaths then return end
        self.BaseClass:DrawDeathNotice(x, y)
end
 
/*---------------------------------------------------------------------------
Display notifications
---------------------------------------------------------------------------*/
local function DisplayNotify(msg)
        local txt = msg:ReadString()
        GAMEMODE:AddNotify(txt, msg:ReadShort(), msg:ReadLong())
        surface.PlaySound("buttons/lightswitch2.wav")
 
        -- Log to client console
        print(txt)
end
usermessage.Hook("_Notify", DisplayNotify)
 
/*---------------------------------------------------------------------------
Remove some elements from the HUD in favour of the DarkRP HUD
---------------------------------------------------------------------------*/
function GAMEMODE:HUDShouldDraw(name)
        if name == "CHudHealth" or
                name == "CHudBattery" or
                name == "CHudSuitPower" or
                (HelpToggled and name == "CHudChat") then
                        return false
        else
                return true
        end
end
 
/*---------------------------------------------------------------------------
Disable players' names popping up when looking at them
---------------------------------------------------------------------------*/
function GAMEMODE:HUDDrawTargetID()
   return false
end
 
/*---------------------------------------------------------------------------
Actual HUDPaint hook
---------------------------------------------------------------------------*/
function DrawHUD()
        localplayer = localplayer and IsValid(localplayer) and localplayer or LocalPlayer()
        if not IsValid(localplayer) then return end
       
        -- Custom
        Base()
       
        -- Default
        GunLicense()
        Agenda()
        DrawVoiceChat()
        LockDown()
        Arrested()
        AdminTell()
        DrawEntityDisplay()
       
end
hook.Add("HUDPaint", "DrawHUD", DrawHUD)


If this helps as well

[Battelfront HUD [WIP]] lua/autorun/cl_battlefronthud.lua:370: attempt to index global ‘GAMEMODE’ (a nil value)

  1. unknown - lua/autorun/cl_battlefronthud.lua:370

This is in my console when I start a DarkRP game

and this is the code at line 370:


function GAMEMODE:DrawDeathNotice(x, y)
        if not GAMEMODE.Config.showdeaths then return end
        self.BaseClass:DrawDeathNotice(x, y)
end

The GAMEMODE variable is the same as the GM variable but they are valid at only certain times (such as when the server is starting up)

Do something like:


local gm_table = GAMEMODE or GM

And then replace any code where you used GAMEMODE or GM with gm_table variable instead

Thanks I am doing it right now I will tell you how it does :slight_smile:

[editline]6th February 2016[/editline]

Did you mean like this?



function gm_table:DrawDeathNotice(x, y)
        if not GAMEMODE.Config.showdeaths then return end
        self.BaseClass:DrawDeathNotice(x, y)
end


I did add the variable in but it doesn’t work :confused:

R.E.P.L.A.C.E, everything that is either GAMEMODE or GM should be gm_table.

I said just if you needed it, and I then posted the part of the coding that was making errors -,-

It’s obvious we’d need the code, else how do you expect us to know what is causing the trouble? Magic?

Now back on topic:
You still didn’t replace all GAMEMODE/GM occurrences in your code, so of course it’d generate errors.

The problem is that you’re still using
[lua]if not GAMEMODE.Config.showdeaths then return end[/lua]
instead of
[lua]if not gm_table.Config.showdeaths then return end[/lua]

and the same kind of error will show up everywhere you didn’t replace GAMEMODE/GM for gm_table.

That didn’t do the tick still getting the lua error:




[BF2Hud v3.1] lua/autorun/cl_battlefronthud.lua:370: attempt to index local 'gm_table' (a nil value)
  1. unknown - lua/autorun/cl_battlefronthud.lua:370


Here is line 370:




local gm_table = GAMEMODE or GM

function gm_table:DrawDeathNotice(x, y)
        if not gm_table.Config.showdeaths then return end
        self.BaseClass:DrawDeathNotice(x, y)
end



Oh yeah, you can’t replace gamemode default functions, you have to use hooks for that.

[lua]
local gm_table = GAMEMODE or GM

hook.Add( “DrawDeathNotice”, “StarwarsHUD_DeathNotice”, function()
if not gm_table.Config.showdeaths then return end
self.BaseClass:DrawDeathNotice(x, y)
end )
[/lua]

[editline]asdad[/editline]
[del]anyways, as long as you don’t edit DarkRP core files you shouldn’t need to do this as it does by itself (Iirc)[/del]
no it does not, the default hud does it and if you disable it then no death notices…
Use the hook then and see if it works.

[editline]6th February 2016[/editline]

Also, when is this HUD being loaded (before, after or during Initialization)?

Every time I install my addon I restart gmod make sure its enabled and start a new game.

[editline]6th February 2016[/editline]



[BF2Hud v4] lua/autorun/cl_battlefronthud.lua:378: attempt to index upvalue 'gm_table' (a nil value)
  1. v - lua/autorun/cl_battlefronthud.lua:378
   2. Run - lua/includes/modules/hook.lua:84
    3. HUDPaint - gamemodes/base/gamemode/cl_init.lua:77
     4. unknown - gamemodes/sandbox/gamemode/cl_init.lua:75



line 378:



local gm_table = GAMEMODE or GM

hook.Add( "DrawDeathNotice", "StarwarsHUD_DeathNotice", function()
        if not gm_table.Config.showdeaths then return end
        self.BaseClass:DrawDeathNotice(x, y)
end )


[editline]6th February 2016[/editline]

Instead of rating it funny maybe you could help Luke xD

Try changing
[lua]local gm_table = GAMEMODE or GM[/lua]
to
[lua]local gm_table = gmod.GetGamemode()[/lua]

EDIT: Coffee, since you’ve rated me dumb, ofc you know how to solve this problem right? And of course you already posted the solution, right? Wait, no you didn’t. So instead of going around rating, try to be at least 0.1% useful.

That didn’t work as well (same lua error:



[BF2Hud v6] lua/autorun/cl_battlefronthud.lua:378: attempt to index upvalue 'gm_table' (a nil value)
  1. v - lua/autorun/cl_battlefronthud.lua:378
   2. Run - lua/includes/modules/hook.lua:84
    3. HUDPaint - gamemodes/base/gamemode/cl_init.lua:77
     4. unknown - gamemodes/sandbox/gamemode/cl_init.lua:75


:confused:

and Luke

Be more useful -,-

Why don’t you use the HUDReplacement Module by DarkRP?
Stupid to override everything without any knownledge.

Alright, I was making this for a server does that mean I just give them the folder with materials/vgui/ and the lua/autorun/