Custom Scoreboard Trouble - and I've barely begun!

I’m diving head first into UI design - probably not a good idea but Neth forced me to.

Ignore the actual vgui stuff - I’m getting


... attempt to index global 'GAMEMODE' (a nil value)

But only when I join the game, if I then re-save the code while in the game, it doesn’t error - but pressing tab only shows the default scoreboard.

If I use GM instead of GAMEMODE:


... attempt to index global 'GM' (a nil value)

I get this error when I load in and save the file.

So - What am I meant to use, GM or GAMEMODE - and why is the default scoreboard still showing. ( note: GM/GAMEMODE is my worst enemy - so a good explanation would be useful :wink: - I’m under the impression GM is only available on server start, then it moves over to GAMEMODE ( for some weird reason unknown to everyone it seems ) - so surely GAMEMODE should be what I need to use ).



print("Loaded Scoreboard")

SB = {}
SB.VGUI = {}

SB.Config = {}
SB.Config.Resize = 100

function GAMEMODE:ScoreboardShow()
	
	SB.VGUI.Container = vgui.Create( "DPanel" )
	SB.VGUI.Container:SetPos( 25, 25 )
	SB.VGUI.Container:SetSize( ScrW()-50, ScrH()-50 )

	SB.VGUI.Header = vgui.Create( "DPanel", SB.VGUI.Container )
	SB.VGUI.Header:SetPos( 0, 0 )
	SB.VGUI.Header:SetSize( 25, 25 )

	return true

end

function GAMEMODE:ScoreboardHide()

	SB.VGUI.Container:Remove()

end


Note: If I’m being a absolute mong then just call me out on it - I can take it!



local bg

function SBoardOpen()
    bg = vgui.Create("DFrame")
    bg:Center()
end

function SBoardClose()
    bg:Remove()
end

hook.Add("ScoreboardShow", "Showit", SBoardOpen)
hook.Add("ScoreboardHide", "Hideit", SBoardClose) 


Edit: Ignore my previous code I’ve updated it! Don’t really know why I didn’t use hook.Add, thanks for the correction Shenesis :stuck_out_tongue:

GM only exists when the script is first opened/lua refresh. GAMEMODE exists only after that happens.
To fix your problem; instead of overriding the gamemode hooks, use

hook.Add and return false at the end of the two hooks

Thanks Shenesis - it works now :slight_smile:

Another question then:



	SB.VGUI.Header.Paint = function()
		draw.RoundedBox( 0, 0, 0, self:GetWide(), self:GetTall(), Color( 0, 0, 0 ) )
	end


Why do I get:


attempt to index global 'self' (a nil value)

I thought you could use self there?

You haven’t passed self through, may I suggest this as a better way imo?



SB.VGUI.Header.Paint = function(self, w, h)
    draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 0 ) )
end


Ah, you have to pass it through :stuck_out_tongue:

But also, on here?


SB.VGUI.Header:SetSize( SB.VGUI.Header:GetParent():GetWide(), 50 )

Should you not be able to do…


SB.VGUI.Header:SetSize( self:GetParent():GetWide(), 50 )

[editline]25th April 2016[/editline]


 local self = SB.VGUI.Header

-- Here is me using it in some of my derma

local divider = vgui.Create("DPanel", bg)
local self = divider
divider:SetSize(bg:GetWide() - bg:GetWide() * 0.18, bg:GetWide() * 0.005)
divider:SetPos(bg:GetWide() * 0.18, bg:GetTall() * 0.33 )
divider.Paint = function ()
     draw.RoundedBox(0, 0, 0, self:GetWide(), self:GetTall(), Color(43,43,43))
end