Name too long. string.len not working.

So I want it that if a players name is too long then cut it off and add some 3 dots.
Right now this is my current code:

local function drawName()
	local DrawName = LocalPlayer():Nick() or ""
	draw.DrawText(DrawName, "NameFont", 85, ScrH() - 90, Color(255,255,255,255), TEXT_ALIGN_CENTER)
	draw.DrawText(string.len("hello"), "NameFont", 20, 0, Color(255,255,255,255), TEXT_ALIGN_CENTER)

	if string.len(DrawName) > 9 then
		DrawName = string.sub(DrawName, 1, 6) .. "..."
		DrawName = string.format("%9s", DrawName)

local function DrawHUD()

This code has been cut for this example.

Should do it by deafult in dlabel is the text is too long. Just shorten the box.

It’s not. I just want to know why the string.len is not working

How is it not working? How do you know it’s not working?

It’s not cutting the text off and replacing it with 3 dots

Try printing out the return value of string.len(DrawName) and see if it’s truely the culprit

Its counting the string.

You’re drawing the name before you have a chance to override it.

Put the draw.DrawText after you do your checks.

Also string.len won’t give accurate results for weird alt names, you can use utf8.len for that.

Gave me this error:

[ERROR] addons/mattehud/lua/autorun/client/cl_main.lua:102: bad argument #1 to ‘len’ (string expected, got nil)

  1. len - [C]:-1
  2. drawName - addons/mattehud/lua/autorun/client/cl_main.lua:102
    3. fn - addons/mattehud/lua/autorun/client/cl_main.lua:235
    4. unknown - addons/ulib/lua/ulib/shared/hook.lua:110

Never mind I forgot to put the DrawName before the check\

Works great now. Thanks

Did you put the DrawName local after you run your checks?

Keep it where it was, only move the draw.DrawText after the checks.

EDIT: no problem