How to generate Hexadecimal codes?

Hello, I’m working in an in-game Server system which may take me a long time.

If I’m being honest, I asked ChatGPT to do this code for me as I have no idea on how to generate random hexadecimal codes; any attempt to help would be appreciated!

This is the code:

local function generateServerCode()
	local randomHex = string.format("%X", math.random(0, 0xFFFFFFFF) )
	local fixedLength = 6
	return randomHex:sub(1, fixedLength)
end
1 Like

From what I can tell with a quick test math.random can’t work with numbers that large. You are already capping the length out at 6 you could just do this:

local function generateServerCode()
	return string.format("%X", math.random(0, 0xFFFFFF))
end 

If you want to use larger numbers you’ll have to use Random

1 Like

Thank you! It seems to work correctly, one of the other alternatives I had thought had like 5 lines lol.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.