Hey, so i've just made a random number generator, and i want to divide a number like (1984) into an x-cordinat and a z cordinat like x=19 and z=84. I've search all over the web for a fix, but i can't seem to find one. Can you help me?
To extract certain digits of a number, we need to use:
tostring
to convert the number into a stringstring.sub
to extract certain characters from the stringFirst, we should convert the number to a string using tostring
:
local coords = 1234 local str_coords = tostring(coords)
Now, we can use string.sub
to get the first and last two characters from the string:
local x_coord = str_coords:sub(1, 2) -- The extraction is from the first to second character. local y_coord = str_coords:sub(3, 4) -- The extraction is from the third to fourth character.
Notice our use of string:sub
; all strings have the full string library built into them as methods, so this is an easier to use alternative to string.sub(str_coords, 1, 2)
and string.sub(str_coords, 3, 4)