How to dissect and parse a string in lua?

后端 未结 3 1511
南旧
南旧 2021-01-25 13:58

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

3条回答
  •  没有蜡笔的小新
    2021-01-25 14:36

    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)
    

提交回复
热议问题