[Lua Help] Shorten Variables and error

Hello, Facepunch!

I’m coding a hud for darkrp and some players have really long names so how do i shorten their names?

Then i use some of my variables i keep getting this error:



[ERROR] lua/includes/modules/draw.lua:73: bad argument #1 to 'GetTextSize' (string expected, got nil)
  1. GetTextSize - [C]:-1
   2. SimpleText - lua/includes/modules/draw.lua:73
    3. DrawPlayerInfo - gamemodes/darkrp/gamemode/client/hud.lua:304
     4. DrawEntityDisplay - gamemodes/darkrp/gamemode/client/hud.lua:370
      5. unknown - gamemodes/darkrp/gamemode/client/hud.lua:403


Code:




	if ply:GetNWString("usergroup") == "Owner" then
		local RankColor = "Color(156, 35, 35)"
		local RankText = "Owner" 
	elseif ply:GetNWString("usergroup") == "Moderator" then
		local RankColor = "Color(155, 105, 35)"
		local RankText = "Moderator"
	elseif ply:GetNWString("usergroup") == "Vip" then
		local RankColor = "Color(45, 15, 155)"
		local RankText = "V.I.P"
	elseif ply:GetNWString("usergroup") == "Admin" then
		local RankColor = "Color(45, 160, 30)"
		local RankText = "Admin"
	elseif ply:GetNWString("usergroup") == "superadmin" then
		local RankColor = "Color(120, 30, 160)"
		local RankText = "Superadmin"
	else
	end





draw.SimpleText(RankText, "DarkRPHUD2", pos.x, pos.y + 20, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)


Can someone please help? Thanks!

I don’t understand why you would draw a players rank on a HUD…

Also, I would suggest learning how variables actually work within scopes because currently the code you posted doesn’t make any sense at all.

Here is an example of how a local variable works.
[lua]
local var = 1
local rank = “admin”
if rank == “admin” then
var = 2
end
print(var)

–returns 2
–returns 1 if rank != “admin”
[/lua]

The way you coded it makes it so the local variable can only be used within something like this:
[lua]
local var = 1
local rank = “admin”
if rank == “admin” then
local var = 2
end
print(var)
–returns 1 because print(var) is not in the scope where it equals 2
[/lua]

Yeah i see im dumb, do you know how to shorten variables too? /:

Just use a shorter name?

You’re really not being clear

[lua]local str = “A short variable name”
local AnUnnecessarilyLongVariableName = “A long variable name”[/lua]



draw.SimpleText(ply:Nick(), "DarkRPHUD2", pos.x + 1, pos.y - 7, Color(0, 0, 0, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)


I want it to shorten ply:Nick() so it only can be 200 Pixels long or something like that…

Could you just set that function to a variable, then use the variable?

Something like this?
[lua]
if SERVER then
AddCSLuaFile()
else
local ply = LocalPlayer()
local name = ply:Nick()
local font = “DermaDefault”
local maxW = 200
surface.SetFont(font)
local w,h = surface.GetTextSize(name)
if w > maxW then
name = string.sub(name, 1, name:len()-3)…"…"
end
print(name)
end
[/lua]

I was about to post similar code.

Next time try to be more clear. Vague questions call for vague answers.