I want to make the script get text after the second space. So for example I type :pm PlayerName [MESSAGE HERE]. I need it to save the playername as a separate variable as well as the message? I have no clue how this is possible without using string.sub but is there an easier way?
COMMUNITY QUESTION
Could someone help me brainstorm this? Getting text after a space?
1 answer
Here is a way of using string pattern to turn each string after a space into a table.
-- The string we want to test this on
local str = ":pm PlayerName [MESSAGE HERE]";
-- The "arguments" table
local tab = {};
-- Starting the string after the ":" (the prefix) so it doesn't get included
str = str:sub(2)
-- Getting each text after whitespace (which %S+ is for)
for w in string.gmatch(str, "%S+") do
-- Inserting each word into the arguments table "tab"
table.insert(tab, w)
end
-- The command
local command = tab[1];
-- The playerName
local playerName = tab[2];
-- The message to send
local message = table.concat(tab, " ", 3)
print("Wants to send", playerName, "a message:", message)
This will of course require you to do some modifications, but it is a basic example of how to accomplish something like this. I recommend you check out the following articles:
String patterns: http://wiki-origin.roblox.com/index.php?title=String_pattern String patterns reference: https://developer.roblox.com/articles/string-patterns-reference
This discussion is preserved as it was. New answers, comments, and votes are closed.
