How to read a certain number of characters, document and loop this proscess

  1. What do you want to achieve? Keep it simple and clear!

I’m looking to create a script that reads a value with many numbers in sets of five (ex: 00005000190004200001…) and document this (ex: 1 = 00005, 2 = 00019, 3 = 00042…)

  1. What is the issue? Include screenshots / videos if possible!

Through my searches I haven’t been able to find an exact term to look up to find a solution to this, however I know its possible.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Google searches of different varieties, I’ve tried using pairs however could not figure out how to find a way to read the number and document it in pairs of five, however I do believe this is the solution based on how it seems to work. If I’m correct I’d need it explained in the terms of the steps i would need to take to achieve this.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

I’m trying to create a card game, similar to Card Wars from Adventure Time (S4EP14 for reference lol). I’m using a Int val to store each card you own, (ex if you own cards #1, 94, 27, and 85, the int would look like this: 00001000940002700085) however within this process I’ve gotten to the point of reading the data to be displayed. After many google searches, and I will continue to search after posted, I have not found a way to properly have a script decipher this into sets of 5 numbers. If there are any ways I can optimize this a comment would be appreciated!

Is it a number or a string? If it’s a number, make sure to test if you can actually have 9999999999999999 as a number, because I don’t think you can.

I’m using a int value now however if I have to convert it to a string than read it as a int that works too, but what you’re saying is there’s a max on the limit of digits allowed in a int value?

After checking, you’re right there is a int-cap so I will be using a string and reading it as an int in this situation

Why not just a table? Integers have precision limits anyway.
Also note that 000010000200003’s string representation becomes 10000200003.

{1, 94, 27, 85}

However, to answer your question:

local input = tostring(00001000940002700085)
-- Left pad the input with zeroes, then iterate over every 5 characters
for card in string.gmatch(string.format(string.format("%%0%ii", math.ceil(#input/5)*5), input), "(%d%d%d%d%d)") do
    print(card)
end

I’m not using a table because I haven’t yet found out how to use Data Store Service to store one, however I’ll research that now, and interesting I didn’t know it was flipped in Lua, but thank you so much for the code, however could I just ask you to break down what’s going on to get to this solution for future reference? For this im mostly asking what gmatch, formating twice, and the “%%0%ii” exactly do. But thank you again!

It isn’t flipped in Lua. It’s just that zeroes to the left of a number are ignored.

string.gmatch( -- given an input, iterate over matches of a pattern
    string.format(
        string.format(
            "%%0%ii", -- example: rounded length is 20, so this becomes %020i
            math.ceil(#input/5)*5 -- round the length of the input to the biggest multiple of five
        ), -- this produces a valid format string that will pad the input text with zeroes
        input -- string.format input
    ),
    "(%d%d%d%d%d)" -- iterate over every 5 characters
)

I’ve tried my best to explain the code. You are likely to understand it more if you read String Patterns and Formatting and Converting Strings.

Oh really? Than I have no idea where I got my information haha, but thank you so much that’s perfect!

Hey, a little further in now and I’m getting an error after the 3rd number, here’s a clip of it and a screenshot of the code in this spot

This is working for me.

local subjectString = "00005000190004200001"

for subString in subjectString:gmatch(".....") do
	print(subString)
end

Remember that leading zeros are removed from numbers when they are converted into strings via the tostring() global function.

That worked perfectly! Its still weird to me that the solution posted before this didn’t work in some circumstances, but that solved the problem, thank you again!