Error When Saving Files

So, I have been working on my HUD, and I ran across this error.



[ERROR] addons/darkrpmodificationaddon/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:90: attempt to concatenate local 'Wallet' (a nil value)
  1. v - addons/darkrpmodificationaddon/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:90
   2. unknown - lua/includes/modules/hook.lua:84


This error only appears when I save any file within my server. Even if I am editing a completely different file. If I save a file, that error is spammed randomly anywhere from 1-5 times.

I have no idea why it would give me this error when saving files.

P.S. - There are no errors when a player joins the game, or when the server is restarted.

This is my hud code, excluding my drawing variables.


--[[---------------------------------------------------------------------------
Which default HUD elements should be hidden?
---------------------------------------------------------------------------]]

local hideHUDElements = {
	-- if you DarkRP_HUD this to true, ALL of DarkRP's HUD will be disabled. That is the health bar and stuff,
	-- but also the agenda, the voice chat icons, lockdown text, player arrested text and the names above players' heads
	["DarkRP_HUD"] = true,

	-- DarkRP_EntityDisplay is the text that is drawn above a player when you look at them.
	-- This also draws the information on doors and vehicles
	["DarkRP_EntityDisplay"] = false,

	-- DarkRP_ZombieInfo draws information about zombies for admins who use /showzombie.
	["DarkRP_ZombieInfo"] = false,

	-- This is the one you're most likely to replace first
	-- DarkRP_LocalPlayerHUD is the default HUD you see on the bottom left of the screen
	-- It shows your health, job, salary and wallet, but NOT hunger (if you have hungermod enabled)
	["DarkRP_LocalPlayerHUD"] = true,

	-- If you have hungermod enabled, you will see a hunger bar in the DarkRP_LocalPlayerHUD
	-- This does not get disabled with DarkRP_LocalPlayerHUD so you will need to disable DarkRP_Hungermod too
	["DarkRP_Hungermod"] = false,

	-- Drawing the DarkRP agenda
	["DarkRP_Agenda"] = false
}

-- this is the code that actually disables the drawing.
hook.Add("HUDShouldDraw", "HideDefaultDarkRPHud", function(name)
	if hideHUDElements[name] then return false end
end)

-- if true then return end -- REMOVE THIS LINE TO ENABLE THE CUSTOM HUD BELOW

--[[---------------------------------------------------------------------------
The Custom HUD
only draws health
---------------------------------------------------------------------------]]

surface.CreateFont( "Legacy2", {
    font = "Arial Black",
    size = 32,
    weight = 5000,
    blursize = 0
} )

surface.CreateFont( "Legacy", {
    font = "Tahoma",
    size = 16,
    weight = 5000,
    blursize = 0
} )


local function DrawHud()
if !LocalPlayer():IsValid() then return end

    surface.SetFont( "Legacy" )

    local Health = LocalPlayer():Health()
    
    if Health <= 0 then Health = 0
    end
    
    local Wallet = LocalPlayer():getDarkRPVar("money")
    
    local Job = LocalPlayer():getDarkRPVar("job")

    local Salary = LocalPlayer():getDarkRPVar("salary")
    
    local FPS = math.Round(1 / FrameTime())
    
    local Glow = math.abs(math.sin(CurTime() * 1) * 255)
    
-- Draw Elements Are Here
    
end
hook.Add("HUDPaint", "DarkRP_Mod_HUDPaint", DrawHud)

function hidehud(name)
	for k, v in pairs({"CHudHealth", "CHudBattery"})do
		if name == v then return false end
	end
end
hook.Add("HUDShouldDraw", "HideOurHud:D", hidehud)

local function DisplayNotify(msg)
    local txt = msg:ReadString()
    GAMEMODE:AddNotify(txt, msg:ReadShort(), msg:ReadLong())
    surface.PlaySound("buttons/lightswitch2.wav")

    MsgC(Color(255, 20, 20, 255), "[DarkRP] ", Color(200, 200, 200, 255), txt, "
")
end
usermessage.Hook("_Notify", DisplayNotify)

Whatever the problem is, it is not caused by the code you provided.

If there’s something you need to help, let me know. I’m stumped.

The error(s) occur during a brief moment of 0FPS as it updates everything. It’s literally half a second. If the update takes longer, there’s more errors. I think that’s what’s happening.
Maybe, is there a way to use an IsValid() statement to see if the getDarkRPVar is valid? I have no idea.



[ERROR] addons/darkrpmodificationaddon/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:90: attempt to concatenate local 'Wallet' (a nil value)
  1. v - addons/darkrpmodificationaddon/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:90
   2. unknown - lua/includes/modules/hook.lua:84

As the error suggests, cl_hudreplacement.lua is the problematic file and 90 is the problematic line. So post 10-20 lines ( lines 80-100 ) from said file ( or lines 0 - 100 ). Or better yet, try to figure it out yourself.


    -- Wallet Box
    
    surface.DrawOutlinedRect( surface.GetTextSize( "Health: " .. Health ) + 6 + 8, 4, surface.GetTextSize( "Wallet: $" .. Wallet ) + 6, 18 )
        
    draw.SimpleText("Wallet: $" .. Wallet, "Legacy", surface.GetTextSize( "Health: " .. Health ) + 6 + 8 + 4, 6, Color(0, 200, 50, 255))

This is the box that is causing the issue.


surface.DrawOutlinedRect( surface.GetTextSize( "Health: " .. Health ) + 6 + 8, 4, surface.GetTextSize( "Wallet: $" .. Wallet ) + 6, 18 )

This is the line causing the issue.

I’ve spent an hour an a half on this issue. Sorry for asking for help, damn.

Replace what you had before with this:



local Wallet = LocalPlayer():getDarkRPVar("money")
surface.DrawOutlinedRect( surface.GetTextSize( "Health: " .. Health ) + 6 + 8, 4, surface.GetTextSize( "Wallet: $" .. Wallet ) + 6, 18 )


I’m sorry, I don’t think I know what you’re trying to say. I don’t see anything different between my code and what you have pasted.

Approximately 2 and a half hours now, I’ve tried everything I can think of. By removing my draw boxes, it removes the issue.
Please note. When I save the file, it only says the error 1-5 times, then everything works like it should with no errors.

— NEW INFORMATION —

This morning, I changed my DarkRPVars from
local Wallet = LocalPlayer().DarkRPVars.money
to
local Wallet = LocalPlayer():getDarkRPVar(“money”)

The problem is…

When I use .DarkRPVars.money, errors spam the console while connecting.
When I use :getDarkRPVar("money), errors are given when I save files in my server.

There has to be something I can do…

The difference is, you need to define to lua what ‘Wallet’ means. If its a local value, make sure its within the same code and defined before you use it. So to fix your issue, find where you’re drawing the text, add this directly above the draw boxes



local Wallet = LocalPlayer():getDarkRPVar("money")


My revised code is as follows:


    -- Wallet Box
    local Wallet = LocalPlayer():getDarkRPVar("money")
    
    surface.DrawOutlinedRect( surface.GetTextSize( "Health: " .. Health ) + 6 + 8, 4, surface.GetTextSize( "Wallet: $" .. Wallet ) + 6, 18 )
        
    draw.SimpleText("Wallet: $" .. Wallet, "Legacy", surface.GetTextSize( "Health: " .. Health ) + 6 + 8 + 4, 6, Color(0, 200, 50, 255))

The same error is being given upon files saves.

P.S. - local Wallet / Job / Salary were all defined within the function that they were called before I revised it, so…

FUCKING YES! After maybe 5-6 hours of being stuck on the stupid shit, I finally found a solution!
I fixed these errors by reverting my…


local Wallet = LocalPlayer():getDarkRPVar("money")

variable back to


local Wallet = LocalPlayer().DarkRPVars.money

Then underneath that, I put…


if not Wallet then Wallet = "" end

So my full definition looked like this:


local Wallet = LocalPlayer().DarkRPVars.money
    if not Wallet then Wallet = "" end

Please note, I did this with my Wallet, Job, and Salary variables.

I feel accomplished.