Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I check for numbers in a certain part of a string?

1 answer

Hey AtlasTheFox,

You can easily achieve what you want with string patterns and string methods. There are many string patterns but, for your case you will need the one for digits which is "%d". There are also various string methods but, for your purposes string.gmatch() should do just fine. Also, you will need to learn about for each iterations. Anyways, below is a decent enough example of how you can just print numbers from a string.

####Example

local str = "CHAN 1 AND 5 AT 255"; -- The string with numbers.

for num in string.gmatch(str, "%d+") do -- For each iteration for all strings that are digits in the number. Also, if you notice I added a '+' at the end, that will get the part of the string where it first notices a number to the end where it reaches a space. If you were to change that to just '%d' it would print each number singularly. Try both out and you will see what I mean.
	print(num) -- prints the part of the string that is a number.
end -- end for the for each iteration.

##Well, I hope I helped in one way or another and the wiki pages are below.

For Each Iterations

String Patterns

String Methods

~~ KingLoneCat

7 comments

0

Thanks for this! I was reading up on this before you posted this answer actually. Again, I really appreciate it!

0

One more question though, is there any way to exclude whatever comes after AT?

0

Yes, string.sub()

0

To exclude what is after "AT" you could use the pattern "(%d+).*AT".

0

^ I said that in my answer.

0

I've gotten it working like I needed it to, thank you very much!

0

No problem also, can you either put [SOLVED] in ur title or accept this answer so that it's not left unanswered anymore?

This discussion is preserved as it was. New answers, comments, and votes are closed.