surface.GetTextSize is nil value... please help..

Hello there, I have been trying to fix this too long now.

I have to get help from here, whenever I join singleplayer with my hud it works fine, but when I try to join my multiplayer server with my hud…
I get this error:



[ERROR] gamemodes/darkrp/gamemode/client/hud.lua:190: bad argument #1 to 'GetTextSize' (string expected, got nil)
1. GetTextSize - [C]:-1
2. DoActualHUD - gamemodes/darkrp/gamemode/client/hud.lua:190
3. DrawHUD - gamemodes/darkrp/gamemode/client/hud.lua:279
4. unknown - gamemodes/darkrp/gamemode/client/hud.lua:394


I don’t know how to fix, here is the line 190 and 189:
[lua]
local job = LocalPlayer().DarkRPVars.job
Width, Height = surface.GetTextSize(job)
[/lua]
And here is one line that uses Width:
[lua]
if Width < 100 then
Snip :3
else
Snip :3
end
[/lua]

Please help me fix this because I have been trying to fix this for days now…

The job is probably nil because it hasn’t initialized.
And, you declare Width as text size, but you use w in the second code snippet?

I used w, h first but now I use Width, Height…
forgot to change the w to Width before posting lol…

could you explain how to fix?

You can do this:
[lua]
local job = LocalPlayer().DarkRPVars.job or “Unemployed”[/lua]

Or this:
[lua]
local width, height = surface.GetTextSize(job or “”) – Remember to make them local.[/lua]

Will try that.
Thanks.

[editline]edit[/editline]
Tried both but none of them worked…
I don’t really get this, it works in singleplayer but not in multiplayer… O_o

[editline]6th February 2013[/editline]

I double post now but any other suggestions?

You have to use surface.SetFont() before using surface.GetTextSize()

It’ll actually work if a font was set beforehand, but this doesn’t always happen.

wait wait wait wait!!
I just saw that it didn’t reload when the HUD was edited -.-
Let me test it with a server restart

[editline]edit[/editline]

Ah didn’t work…
Should this work?



	Width, Height = surface.SetFont("DarkRPHUD2").GetTextSize(job)


[editline]6th February 2013[/editline]

Oh, well sorry that I suck with surface shit

[lua]
surface.SetFont(“Trebuchet18”)
local w,h = surface.GetTextSize( “myText” )
[/lua]

Okey, but I need the darkrp var ‘job’ (LocalPlayer().DarkRPVars.job)…
How should I fix that… :frowning:

[lua]
surface.SetFont(“Trebuchet18”)
local w,h = surface.GetTextSize(LocalPlayer().DarkRPVars.job or “”)
[/lua]

Will try that.

[editline]edit[/editline]
Didn’t work…
Thanks anyway, I really need to fix this…
I HAVE to get it fixed somehow, or else the text of a job will go outside the box of my custom HUD…
The point with this is to expand the box to a size depending if the text is wider then 100…

Someone got any other solution to expand the box depending on text length?