Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would I create Chat arguments?

Asked by 4 years ago

I'm making this Chat Command Script which enables the player to Teleport players, kick them etc. But one thing I've been stuck on for a long time is this. How would I create the Chat Arguments? For example, how would I be able to separate the string the player sent into sections like, Arg1 = ActionYouWantToDo, Arg2 = TragetedPlayer. Sorry if I dint explain this too well

1 answer

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

I like using string.gmatch for this thing.

Explanation on the key: %s will get a matching white space in the given string. ^%s will simply reverse the pattern, which means it'll get a list of matching letters instead. Wrapping to [^%s]+, this means it'll get each of every words that are not spaces or blanks. Plus means more!

Check out LUA String Patterns

The script below iterates through the matching words and store the command in a variable, and the rest goes in a table.

The loop will basically retrieve a list of words separated by blank spaces in between.

--EXAMPLE MSG
local message = ':beautiful me -1'

local command
local args = {}

local key = '[^%s]+'

for a in string.gmatch(message, key) do
    if not command then
        command = a
    else
        table.insert(args, a)
    end
end

--The rest.

Once you got the command and the arguments list (don't worry, they're ordered), the rest of the code is up to you. Goodluck!

Ad

Answer this question