Problem with hud (attempt to index global 'surface')
10 replies, posted
My HUD is being a problem. I made my first hud and it worked with no problem. I changed the colors and changed how it looked slightly, and the next time I started it up I got an error:
[code]
[ERROR] gamemodes/modernrp/gamemode/shared.lua:7: attempt to index global 'surface' (a nil value)
1. unknown - gamemodes/modernrp/gamemode/shared.lua:7
[/code]
The error is apparently related to the font I made. However, the font was fine the last time I used it (before editing my HUD) and now it says it's having an error. I can only assume I am doing something wrong in cl_init but I have no idea. I'll post the necessary files related to the HUD. It's probably a very simple error and I'll probably feel stupid after this:
[lua]
GM.Name = "Cooperative Association Role-Play"
GM.Author = "Steven"
GM.Email = "N/A"
GM.Website = "N/A"
function GM.Initialize()
surface.CreateFont( "Trebuchet34",
{
font = "Trebuchet24",
size = 34,
weight = 1000,
antialias = true,
shadow = false,
})
surface.CreateFont( "Trebuchet64",
{
font = "Trebuchet24",
size = 64,
weight = 1000,
antialias = true,
shadow = false,
})
end
[/lua]
cl_init.lua
[lua]
include( "shared.lua" )
include( 'sh_player.lua' )
-- Hud
function hud() -- Consider functions like a cyber button, and everything INSIDE the function turns on when this cyber button is pressed.
local health = LocalPlayer():Health()
local armor = LocalPlayer():Armor()
local percent = "%"
local cash = LocalPlayer():GetMoney()
draw.RoundedBox(1, 207.5, ScrH() - 60 - 20, 300, 40, Color(255,255,255,255))
draw.RoundedBox(1, 207.5, ScrH() - 60 - 20, health * 3, 40, Color(255,97,97,255))
draw.RoundedBox(1, 535, ScrH() - 60 - 20, 300, 40, Color(255,255,255,255))
draw.RoundedBox(1, 535, ScrH() - 60 - 20, armor * 3, 40, Color(97,110,255,255))
local avatar = vgui.Create("AvatarImage", frame)
avatar:SetPos(50,ScrH() - 100)
avatar:SetSize(64, 64)
avatar:SetPlayer( LocalPlayer(), 64 )
draw.RoundedBox(1, 45, ScrH() - 105, 74, 74, Color(0,0,0,200))
draw.SimpleText(string.format("Health: %i%s", health, percent), "Trebuchet24", 215, ScrH() - 32.5 - 40, Color(200,200,200,255))
draw.SimpleText(string.format("Armor: %i%s", armor, percent), "Trebuchet24", 542, ScrH() - 32.5 - 40, Color(200,200,200,255))
draw.SimpleText(string.format("$%s", cash), "Trebuchet34", 225, ScrH() - 80 - 40, Color(200,200,200,255))
draw.SimpleText(string.format("%i / %i", LocalPlayer():GetActiveWeapon():Clip1(), LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType())), "Trebuchet64", ScrW() - 250, ScrH() - 100, Color(255,255,255,255))
end
hook.Add("HUDPaint", "MyHudName", hud) -- I'll explain hooks and functions in a second
function hidehud(name)
for k, v in pairs({"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"}) do
if name == v then return false end
end
end
hook.Add("HUDShouldDraw", "HideOurHud:D", hidehud)
[/lua]
These are the only two things I've added HUD things in, and the only two I've edited since it got this error. If someone could find the problem, I would appreciate it.
Thanks.
You're trying to use surface in a shared file, without checking to make sure that the server isn't running it, add a if CLIENT then
Thanks, adding that did the trick. However, I'm not sure what is wrong with my code. For some reason the text looks transparent when the transparency is still 255. Here's a picture of what I'm talking about:
[url]http://i.imgur.com/4T9caYU.jpg[/url]
If you look closely, you can see the text "Health: " with the health value on the health bar. However, it's barely visible. I set it in the code so it should be grey-ish, but instead it's about the same color as the bar it seems. As you can see, you can't even see the text for the armor, which should be showing up, as the back of the bar is white and the text is grey. This is the cl_init with the edited stuff:
[lua]
include( "shared.lua" )
include( 'sh_player.lua' )
-- Hud
function hud() -- Consider functions like a cyber button, and everything INSIDE the function turns on when this cyber button is pressed.
local health = LocalPlayer():Health()
local armor = LocalPlayer():Armor()
local percent = "%"
local cash = LocalPlayer():GetMoney()
draw.RoundedBox(1, 207.5, ScrH() - 60 - 20, 300, 40, Color(255,255,255,255))
draw.RoundedBox(1, 207.5, ScrH() - 60 - 20, health * 3, 40, Color(255,97,97,255))
draw.RoundedBox(1, 535, ScrH() - 60 - 20, 300, 40, Color(255,255,255,255))
draw.RoundedBox(1, 535, ScrH() - 60 - 20, armor * 3, 40, Color(97,110,255,255))
local avatar = vgui.Create("AvatarImage", frame)
avatar:SetPos(50,ScrH() - 100)
avatar:SetSize(64, 64)
avatar:SetPlayer( LocalPlayer(), 64 )
draw.RoundedBox(1, 45, ScrH() - 105, 74, 74, Color(0,0,0,200))
draw.SimpleText(string.format("Health: %i%s", health, percent), "Trebuchet24", 215, ScrH() - 32.5 - 40, Color(50,50,50,255))
draw.SimpleText(string.format("Armor: %i%s", armor, percent), "Trebuchet24", 542, ScrH() - 32.5 - 40, Color(50,50,50,255))
draw.SimpleText(string.format("$%s", cash), "Trebuchet34", 225, ScrH() - 80 - 40, Color(200,200,200,255))
draw.SimpleText(string.format("%i / %i", LocalPlayer():GetActiveWeapon():Clip1(), LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType())), "Trebuchet64", ScrW() - 250, ScrH() - 100, Color(255,255,255,255))
end
hook.Add("HUDPaint", "MyHudName", hud) -- I'll explain hooks and functions in a second
function hidehud(name)
for k, v in pairs({"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"}) do
if name == v then return false end
end
end
hook.Add("HUDShouldDraw", "HideOurHud:D", hidehud)
[/lua]
What did I do wrong?
EDIT: It seems to be related to the color, I thought the last option was transparency? If I set everything as 255 I can see it, but if I set it to 0,0,0,255 it becomes invisible?
bump - please help
Works fine for me [URL]http://puu.sh/aesZE/e47e25b396.jpg[/URL]
Although, the Lua errors are because the fonts Trebuchet34 and Trebuchet72 don't exist, did you create them yourself? If so, post the code.
EDIT: I changed the font names and the HUD works fine [url]http://puu.sh/aet7z/c987419413.jpg[/url]
What lua errors? I'm not receiving errors anymore, it's simply not working correctly. Both seem to be getting transparent the closer it gets to white, and if I make it black it's transparent black. I have no idea what's wrong. Plus you must have mistyped, as there is no Trebuchet72, only 34 and 64, and both are in the shared.lua file I provided above.
EDIT: I also added that thing he said before btw, those are probably the errors you are talking about. This is it:
shared.lua
[lua]
GM.Name = "Cooperative Association Role-Play"
GM.Author = "Steven"
GM.Email = "N/A"
GM.Website = "N/A"
function GM.Initialize()
if CLIENT then
surface.CreateFont( "Trebuchet34",
{
font = "Trebuchet24",
size = 34,
weight = 1000,
antialias = true,
shadow = false,
})
surface.CreateFont( "Trebuchet64",
{
font = "Trebuchet24",
size = 64,
weight = 1000,
antialias = true,
shadow = false,
})
end
end
[/lua]
Yeah, my bad it is because in the stack trace it mentioned 72 so I misread it.
Your HUD works fine for me though, you have tried reconnecting?
EDIT: Using your exact code, it works fine for me [url]http://puu.sh/aetD5/5780d941c5.jpg[/url]
Well, I have no idea what's wrong. I don't see anything different between the codes I provided here and the codes I'm using. I'll just post my entire gamemode (since it's small) and see if anyone can figure it out.
[url]https://dl.dropboxusercontent.com/u/85985593/gamemode.rar[/url]
function GM.Initialize() should be "function GM:Initialize()"
Fixed that, but it didn't fix the problem with the text. Any idea about the text?
bump - anyone?
Sorry, you need to Log In to post a reply to this thread.