Help with using tables

I’m making a custom name gui using a spritesheet, several ImageLabels and ImageRectSize and ImageRectOffset to make the letters. I currently have a table set with the ImageRectOffset for each letter/number, and I don’t know how I can take a letter from the users DisplayName and find it on the table and return the value assigned to it. Also, I would like to know how to filter the characters depending on whether they are a number or not. I googled something that was supposed to do that, but it isn’t working. Any help would be appreciated.

local username = game.Players.LocalPlayer.DisplayName -- User's display name

local charactercount = 0 -- I only have 5 images for the name, so I want to make sure it doesn't error if the display name is greater than 5

local lettertable = { -- The table
	Zero = "0,0",
	One = "8,0",
	Two = "16,0",
	Three = "24,0",
	Four = "32,0",
	Five = "40,0",
	Six = "48,0",
	Seven = "56,0",
	Eight = "64,0",
	Nine = "72,0",
	A = "0,8",
	B = "0,16",
	C = "0,24", 
	-- I'll add the rest once this works
}

for character in username:gmatch(".") do -- Just learned what :gmatch() is
	charactercount += 1 -- Add to the count
	if typeof(character) == "number" then -- Aparentally this will tell me if it's a number or not? (I rely on google too much)
		print(character.." Is a number") -- Doesn't print
	elseif character == "_" then
		
	else
		table.find(lettertable,character) -- Finding the value in the table from the letter
	end
end
1 Like

Hi there.

If you change your table keys (the left of each value assignment) to be the exact string which you would receive from text, you can easily do this.
Like so:

local lettertable = {
	["0"] = "0,0",
	["1"] = "8,0",
	["2"] = "16,0",
	["3"] "24,0",
	["4"] = "32,0",
	["5"] = "40,0",
	["6"] = "48,0",
	["7"] = "56,0",
	["8"] = "64,0",
	["9"] = "72,0",
	A = "0,8",
	B = "0,16",
	C = "0,24", 
}

It’s crucial that the numbers are wrapped in [""] as otherwise you will get a syntax error. The rest can be written like normal.

Then simply index the table, like so:

local text = "ABC123"
for index = 1, #text, 1 do
	local character = text:sub(index, index)
	print(lettertable[character])
end


Also for custom font rendering I have to recommend my awesome, open-source module; Text+.
It’s super efficient, easy, and powerful.

3 Likes

Thanks for the reply, I’ll test this out in a moment. I’ve seen your custom font renderer, but I don’t know if it’ll work with the font I’m using.

You beat me to it, my approach would be to build a dictionary table where each character including digits is directly mapped to its sprite offset for example map["A"] = Vector2.new(0,8)
then iterate through the player’s DisplayName one character at a time via string.sub or :gmatch, use lua %d vs %a patterns to distinguish digits from letters and finally grab the correct offset with map[char] to set ImageRectOffset

It works with any font. But to automate the Lua data table process you need an actual font file; .ttf or .otf. This makes it perfect for fonts you’ve found online.

You should be able to find a tutorial online as for how to convert your font spritesheet to a font file.

that table driven approach works with any fixed grid sprite sheet, just map each character to its exact offset in your table, iterate the DisplayName one character at a time
and directly index that table to set ImageRectOffset

1 Like

I figured it’d have to be a font file, but I thought fonts had to be just black and white?

They don’t have to be perfectly white, but that’s recommended, as then you can ensure that your specified color will be the exact one shown.

If you don’t plan to modify the color of that font, it will of course work perfectly fine still to keep it colored.

Thanks for making me aware of this possible misunderstanding.
I will update the documentation accordingly.

I’d appreciate it if you’d mark my reply as Solution if it works out for you. :slight_smile:

I see. After I test the suggestion you posted up top, (I like to make things myself before I use public resources for learning purposes) I’ll try out Text+

I feel you.
But trust me when I say that Text+ is worth it — it’s hundreds of hours of work, for completely free!

If you’re curious, try inspecting the source code. It’s quite advanced and written for performance, so it might not be super readable, but I did my best by adding lots of detailed comments.

1 Like

I’m having trouble finding tutorials to convert it into a font file, do you know any?

I see that you have a bit of a problem. Fonts are usually made of vectors, not bitmaps (images).

Found a good tutorial for vectors, but I’m not sure if you’re able to follow when using images. You could try and let me know — when he says to copy and paste the vector, try to copy and paste the relevant part of your sprite-sheet.

1 Like

Actually, I think I found a way. In the text+ documentation, it links to snowb, which lets me upload images as glyphs.

1 Like

That’s right! I forgot to mention that the actual requirement for Text+ is an XML file in BMFont format, just like what SnowB provides, which is why I link to SnowB.

I know there’s already a solution, but I have another issue with the table. I’ve been trying to set the ImageRectOffset but it keeps defaulting to 0,0. I assume it’s because the ImageRectOffset needs a Vector2 property, but trying to do label.ImageRectOffset = Vector2.new(lettertable[character]), just gives this error:
Screenshot 2025-05-09 174139

Code
local username = game.Players.LocalPlayer.DisplayName

local charactercount = 0

local lettertable = {
	["0"] = "0,0",
	["1"] = "8,0",
	["2"] = "16,0",
	["3"] = "24,0",
	["4"] = "32,0",
	["5"] = "40,0",
	["6"] = "48,0",
	["7"] = "56,0",
	["8"] = "64,0",
	["9"] = "72,0",
	A = "0,8",
	B = "0,16",
	C = "0,24",
	N = "16,24"
	 
}

local text = "ABC123"
for index = 1, #text, 1 do
	if charactercount < 5 then
		local character = text:sub(index, index)
		local label = script.Parent:FindFirstChild("HealthLabel"..tostring(charactercount))
		print(lettertable[character])
		label.ImageRectOffset = Vector2.new(lettertable[character])
		charactercount += 1
	end
end