Admin commands string.split indexing help

Keeping this clear and simple.

I’m making some admin commands, and I want to know how I can get a full sentence after two characters. It’s confusing, I don’t know a better way to explain it.

“!kick Chaddaking being a bad player”

Using string.split I can get the target of the command (Chaddaking, me) and the command itself (kick) and a part of the kick reason.

string.split("!kick Chaddaking being a bad player", " “)[1] = “!kick”
string.split(”!kick Chaddaking being a bad player", " “)[2] = “Chaddaking”
string.split(”!kick Chaddaking being a bad player", " ")[3] = “being”

After the first two indexes ("!kick" and “Chaddaking”) how could I just get everything else on one line? Essentially I’m asking how do I remove the first two indexes from the string.split table.

Please let me know if I need to go further into detail, thanks.

Keep reference of your table,

local Strings = string.split(Message, " ");
--if your command is the first param
local Command = table.remove(Strings, 1)
--if the name of your target is the next
local Name = table.remove(Strings, 1)
--then the rest of the Strings table is the reason
--local Reason = (function() local t = ""; for _,v in ipairs(Strings) do t = t .. " " .. v; end return t end)()
-- However there is probably a better way to do the above line.
-- Someone below posted a better way to concat the string together
local Reason = table.concat(Strings, " ")

Im not sure about this but I think you need to do this:

local kickedPlayer = string.split("!kick Chaddaking being a bad player"," " )[2]
local reason = string.gmatch("!kick Chaddaking being a bad player", kickedPlayer)()

Edit: string.gmatch instead of string.gsub

If I understand your question correctly, you can get all of the splitted strings after the first one and insert them into a table:

local splitTable = string.split("!kick Chaddaking being a bad player", " ")
local command = splitTable[1]
local arguments = {}

for a = 2, #splitTable do
    table.insert(arguments, splitTable[a])

local reason = table.concat(arguments, " ", 2)

print(arguments[1]) -- "Chaddaking"
print(arguments[2]) -- "being"
print(arguments[3]) -- "a"
-- etc.

Thank you for helping, I didn’t know about table.concat and thankfully it exists. Also thank you @AstralBlu_e and @SorxtaKanda too.

For anyone wondering, I shortened it down to:
ExtraParameters = table.concat(string.split(string.sub(Message, 2), " "), " ", 3);

It’s messy but it works as a one-liner.

1 Like