When parsing a string, it's important to define some rules in how your text is structured. In your case, you have a command prefix (what comes before the command), a command operator (the type of command), and a command operand (the arguments of the command) -- in that order. The command prefix is static, so it's pretty easy to check for that case:
local command = ":fly person"
if (command:sub(1, 1) == ":") then
...
end
You know that the characters after the prefix will be the type of the command, separated by a space, then followed by the operand (the effected player's name). Let's look for the type of the command first.
local command = ":fly person"
if (command:sub(1, 1) == ":") then
local commandType = command:sub(2, "%S+")
if (commandType == "fly") then
...
end
end
In the code snippet above, I'm using the character class %S to represent anything that's NOT a space character. The + quantifier just tells the program that I want all characters that aren't a space character, up until the first space character it finds.
The next sequence is similar; I will use the same string pattern as I did before, but I will use it only on the end of the string. This can be achieved by using the $ character, also classified as a magic character, that will apply the string pattern at the end of the string.
local command = ":fly person"
if (command:sub(1, 1) == ":") then
local commandType = command:sub(2, "%S+")
local commandArgument = command:match("%S+$")
if (commandType == "fly") then
doSomeMethod(commandArgument)
end
end
I'll edit this answer a little bit to include some useful resources where you can learn more about string patterns and methods. Until then, I hope this answer was somewhat useful. Let me know if you have any questions.
3 comments
well, you can use string.gmatch
you can use string.find(msg, [player name here])
string.find() returns ints of where the pattern starts and ends