How to connect words when you ".split()"

Hello, I’m making an admin command and I want to give a reason when you use a command.
Ex:

/kick player no cheating

How would I make “no cheating” into one string?
Thank you.

Use table.concat() to connect the strings.

1 Like

How would I get the ignore the others part though? I know I have to loop through the strings and ignore it, but how would I add it?

for _, word in pairs(message) do
   if not word[1] and not word[2] then
      table.concat(word3+)
   end
end

That was an example I don’t if it will work though. :confused:
Thank you I’ll try that

Use the third and fourth parameters to indicate the starting and ending index of the array. In your case, you would only need to use the third parameter to indicate the start.

1 Like

Use the builtin select() function to splice the elements and then concat the end portion
The first parameter tells it at what index to start the splice

local command = "kick player no cheating abc abc abc"
local elements = {}
for element in string.gmatch(command, "([^ ]+)") do
  table.insert(elements, element)
end
local str = table.concat({select(3, unpack(elements))}, ' ')
print(str) --> no cheating abc abc abc
1 Like

There is no need to do all of that. table.concat() allows you to specify starting and ending indices.

1 Like

Can you give me a code example please. I think I got it, but I’m not sure. I got this so far:

table.concat() is a documented function.

1 Like

you originally said table.join and then edited it, mb

1 Like

Following the code sample you sent above: table.concat(message, " ", 3)

  1. The first parameter is the array.
  2. The second parameter is the separator between each string,
  3. The third parameter is the starting index.
1 Like

Just remove the first two arguments out of the split since it’s logically the command and the target.

local commandGiven = "/kick player no cheating"

local split = string.split(commandGiven, ' ')

local reason = table.concat(split, " ", 3)
1 Like

Thank you, I probably should have looked at the document first I didn’t know you have to add the number after

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.