"..attempt to perform arithmetic on local 'num'.."

Hey all, I have been learning to code in LUA for the past few days, I try to fix my problems by myself but it’s got to the point where I am going to punch the screen.

I keep getting the below error and it’s only started happening since I changed my cl_hud.lua file. Can anyone help me?


Hook 'draw_hud' Failed: [lua\includes\extensions\math.lua:213] attempt to perform arithmetic on local 'num' (a table value)

And here is my cl_hud.lua document.


// Hides the default interface, show => CHudWeaponSelection for now though until custom weapon menu is finished
function undraw_hud(nick)
	for k, v in pairs{"CHudHealth", "CHudBattery", "CHudAmmon", "CHudSecondaryAmmo", "CHudDeathNotice", "CHudDamageIndicator", "CHudCrosshair", "CHudAmmo"} do
		if nick == v then return false end
	end
end

hook.Add("HUDShouldDraw", "undraw_hud", undraw_hud)

// Draw the MAIN custom HUD interface
function draw_hud()
	// For some reason, init.lua vars don't work here, so have to set them again, fuck sake!
	local NWPlayerName = LocalPlayer():GetNWString("PlayerName")
	local NWPlayerMoney = LocalPlayer():GetNWInt("PlayerMoney")
	
	// Add some vars like checking if player is alive for use in the HUD
	local playerAlive = LocalPlayer():Alive()
	local playerDead = !LocalPlayer():Alive()
	local playerHealth = LocalPlayer():Health()
	local playerTeam = LocalPlayer():Team()
	local playerCurrentWeapon = LocalPlayer():GetActiveWeapon()
	local playerCurrentWeaponName = LocalPlayer():GetActiveWeapon():GetPrintName()
	
	// Check if player is not joining mode
	local function playerNotJoining()
		if LocalPlayer():Team() != 0 then return true end
	end
	
	// £ sign doesn't work in LUA, couldn't find out why so made my own currency, well, stolen from fallout. Fuck you LUA!
	local moneyName = "Caps"
	
	// Check to see if physgun and other weird weapons are selected, they produce dodgy weapon names, also check to see if hands are equipped
	if (playerCurrentWeaponName) == "Hands" or (playerCurrentWeaponName) == NULL then
		playerCurrentWeaponName = "Nothing"
	elseif (playerCurrentWeaponName) == "#GMOD_Physgun" then
		playerCurrentWeaponName = "Physgun"
	end
	
	// Set measurements for the HUD
	local cinematicBarHeight = 40
	
	local hudPadding = 10
	
	local healthBarWidth = 200
	local healthBarHeight = 15
	local healthBarX = hudPadding
	local healthBarY = cinematicBarHeight + hudPadding
	
	local experienceBarWidth = ScrW() - (hudPadding * 2)
	local experienceBarHeight = 15
	local experienceBarX = hudPadding
	local experienceBarY = ScrH() - cinematicBarHeight - hudPadding
	
	local experienceBarTextX = ScrW() / 2
	local experienceBarTextY = ScrH() - cinematicBarHeight - hudPadding - (experienceBarHeight / 2)
	
	// Check to see if player is dead and draw a dark screen, in future I want to add a heartbeat effect and wobbly screen for effect
	if (playerDead) then
		draw.RoundedBox(0, 0, 0, ScrW(), ScrH(), Color(0, 0, 0, 250))
	end
	
	// Draw the widescreen black lines on top and bottom, haven't tested on low resolutions!
	draw.RoundedBox(0, 0, 0, ScrW(), 40, Color(0, 0, 0, 255))
	draw.RoundedBox(0, 0, ScrH() - 40, ScrW(), 40, Color(0, 0, 0, 255))
	
	// Draw the dark overlay, I want to make this a filmgrain type texture later on, think that would give it a dark feel to the game. Like post-apocolyptic
	draw.RoundedBox(0, 0, 0, ScrW(), ScrH(), Color(20, 20, 20, 80))
	
	// Draw the health bar, only when alive
	if (playerAlive) then
		// Draw the actual health bar
		draw.RoundedBox(10 - 1, 50 - 1, 150 + 2, 15 + 2, Color(0, 0, 0, 150)) 
		draw.RoundedBox(10, 50, 70 - 1, 15, Color(255, 0, 0, 150))
		
		// Draw health text, but remove when health bar is finished
		draw.SimpleText(playerHealth, "DefaultBold", 15, 50, Color(255, 255, 255, 255))
		
		// Draw the experience bar
		draw.RoundedBox(0, 9, ScrH() - 66, ScrW() - 19, 17, Color(0, 0, 0, 150))
		draw.RoundedBox(0, 10, ScrH() - 65, ScrW() - 20 - 500, 15, Color(80, 80, 255, 150))
		
		// Draw the players level
		draw.SimpleText("Level 4", "DefaultBold", ScrW() / 2, ScrH() - 64, Color(255, 255, 255, 255))
	end
	
	// Check to see if player is not joining, then draw their name
	if (playerNotJoining) then
		draw.SimpleText("Name: "..NWPlayerName, "DefaultBold", 10, 13, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT)
	end
	
	// Draw the players current weapon or item if not in joining mode and alive
	if (playerNotJoining) and (playerAlive) then
		draw.SimpleText("Equipped Item: "..playerCurrentWeaponName, "DefaultBold", 10, ScrH() - 27, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT)
	end
	
	// Draw the players money if not in joining mode
	if (playerNotJoining) then
		draw.SimpleText(moneyName..": "..NWPlayerMoney, "DefaultBold", ScrW() - 10, ScrH() - 27, Color(255, 255, 255, 255), TEXT_ALIGN_RIGHT, TEXT_ALIGN_RIGHT)
	end
end

hook.Add("HUDPaint", "draw_hud", draw_hud)


[lua]draw.RoundedBox(10, 50, 70 - 1, 15, Color(255, 0, 0, 150))[/lua]

Missing an argument there.

(That’s in // Draw the health bar, only when alive )

[editline]28th December 2010[/editline]

In fact,

[lua]if (playerAlive) then
// Draw the actual health bar
draw.RoundedBox(10 - 1, 50 - 1, 150 + 2, 15 + 2, Color(0, 0, 0, 150))
draw.RoundedBox(10, 50, 70 - 1, 15, Color(255, 0, 0, 150))
[/lua]

Both missing args

Thanks, I knew it was something really simple, the error really baffled me because it doesn’t say where the error is taking place.

It tells you what line it occurs on. Simply look at that line, see what functions (or calculations or what have you) you are using on that line, and lookup the function on the wiki to make sure you have correct/sufficient args in it.

That error is only specific to the lua\includes\extensions\math.lua though right? Which wouldn’t show me where the code is wrong in my file?

The best thing you can do when you get an error like that in future is to go on http://luabin.foszor.com/code
and find out what function is giving you the error

-Just making sure I’m right here- So basically you want to go to the luabin and find the math.lua, and if its the function for say dividing (just an example) then you know that something is wrong with your division?

Looks like a bit of a paradox there. Tsk tsk tsk tsk tsk.

No, by opening math.lua and finding which function is generating the error, you can figure out what you are doing that is making the error occur.

You know, actually debugging code.