1

I am trying to make command arguments in Roblox. For example, /kill playername. The problem is I don't know how to parse the playername from the string /kill playername. This code is in something like this:

game:GetService("Players").PlayerAdded:Connect(function(Player)
    Player.Chatted:Connect(function(Message)
        if string.sub(1, #Message) == "/kill " then
            --this means the string starts with /kill and is expecting an argument. 
            --How can I parse this argument from the string
        end
    end)
end)

Edit: I want to add /setdata <Playername> <DataToChange eg. money> <Value> Example command:

/setdata MyRobloxUsername Money 10000

I am trying to use something like this to do so

local Command, Playername, DataToChange, Value = string.match(???)

I just need to get the values from the string into variables. I can figure out how to change the data using the variables myself. Just how to get the values from the string. How can I do what I am describing?

I unaccepted the answer because I need further help. Once I get this help I will re accept it. My next request is similar, but with 3 arguments instead of 1. I need help as string:Match() is very counter intuitive to me

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

3 Answers3

2

Use string.match:

Message=" /kill playername  "
command, arg = Message:match("%s*/(.-)%s+(.*)%s*$")
lhf
  • 70,581
  • 9
  • 108
  • 149
  • 2
    This is not an extensible solution. @JulianTiemann I suggest you use a [string split](https://stackoverflow.com/questions/1426954/split-string-in-lua) equivalent that will put separate words in a table. – BotOfWar Sep 08 '19 at 02:41
0

If you want this to be more flexible to more commands in the future, I suggest you take both lhf's and BotOfWar's suggestions and combine them.

local function executeCommandInMessage(message)
    -- do a quick regex of the message to see if it is formatted as a command
    -- all we care about is the command, any arguments are optional.
    local command, arguments = string.match(message, "^/(%w+)[%s]?([%w%s]+)$")

    if command ~= nil then
        -- we've found a command, parse the arguments into groups of non-space characters
        -- then store each word in the parts array
        local parts = {}
        for w in arguments:gmatch("%S+") do
            table.insert(parts, w)
        end

        -- handle each command individually
        if command == "kill" then
            local player = parts[1]
            print(string.format("Killing %s", player))

        elseif command == "setdata" then
            local player = parts[1]
            local value = parts[2]
            local amount = parts[3]
            print(string.format("Setting %s on %s to %s", value, player, amount))

        -- add any further commands to the list..
        -- elseif command == "" then
        end
    end
end


-- listen for any message submitted by players
game:GetService("Players").PlayerAdded:Connect(function(Player)
    Player.Chatted:Connect(function(msg)
        -- check for any commands
        executeCommandInMessage(msg)
    end)
end)

In the future, if you need a better regex to parse the message, I suggest you take a look at how to do Lua pattern matching. They're pretty easy to read once you know what to look at.

Kylaaa
  • 6,349
  • 2
  • 16
  • 27
  • This solution has trouble with commands that do not have arguments. Things like '/nukeAll' will not parse properly. – Kylaaa Sep 17 '19 at 22:38
-1

I suggest splitting the string with the string.split method to get the segments, then check if the first value is what you want.

game:GetService("Players").PlayerAdded:Connect(function(Player)
    Player.Chatted:Connect(function(Message)
        local segments = Message:split(" ")
        if((#segments >= 1) and (segments[1] == "/kill")) then
            -- The rest of the arguments can be accessed like this:
            local args = {unpack(segments, 2)} -- Gets every argument after the first value,
            -- which is the command.
        end
    end)
end)
Zipper
  • 181
  • 1
  • 10
  • 1
    I would upvote but I lost all my 30 reputation for "rude or abusive" –  Oct 04 '19 at 22:18