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

Grabbing Values from a string?

So I've been trying to create a script that processes commands (through player chat) . The system checks for the keyword (the / statement). But if I want to add another argument into the command, let's say an integer. How do I detect that integer within the string?

The problem is simple, and I'm hopefully expecting a simple answer :D ! THX!!!!

1 answer

✓ Accepted answer

I wouldn't exactly call my answer simple, but I'll try to explain as best as I can. I'll show you how this can be accomplished with string.match() and string patterns.

local testString = "/walkspeed 52"

local command,number = string.match(testString,"^/(%a+) (%d+)")
print(command) -- walkspeed
print(number) -- 52

string.match is used to find where in a string that a pattern may exist. In the example our pattern is ^/(%a+) (%d+). First there is the ^, this makes it so it will only check if the pattern exists at the beginning of the string. Next is the / which is our first character to match. After that we have (), these in a string pattern are called captures, it tells string.match that it should return what it matches between them. And last we have %a+ and %d+. %a makes the pattern match a letter and %d makes the pattern match a digit. The + extends the pattern matching to search for one or more character, so %a+ will search for one or more letters.

2 comments

0

This is one of your better answers. Keep it up!

0

Thanks! I'm glad it helped.

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