Getting every index beyond a certain index of a table

Hello! Please bear with me as I’m pretty new to scripting and this is my first post :slightly_smiling_face:

Basically, I’m trying to get everything after a certain index inside a table. For example, I have split a player’s message and I’m trying to get everything after message[4] and print it as a reason:

game.Players.PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(msg)

local message = string.split(message, " ")

if string.lower(message[1]) == 'i' and string.lower(message[2]) == 'like' and string.lower(message[3]) == 'cake' and string.lower(message[4]) == 'because' then

-- the unknown part that needs to print all indexes after [4]
end

The problem is, I have used the API, Google, YouTube yet there’s no answer (or at least I haven’t understood it) also the printing part’s there just to test it out, my main script will originally have to use these extracted indexes together and put them in a variable that can be used in a forced message function:

(game:GetService("Chat"):Chat(Instance, text, Color))

I’d appreciate any redirection that could help me solve this question.

NOTE: I won’t be able to test responses until tomorrow, therefore if I haven’t marked the solution/closed the topic it’s either that I haven’t had the chance to get on yet or haven’t checked the forum.

You can do, for anything AFTER index 4

local wordTable = {} -- i believe this corresponds to your message table
local newTable = {}
for i=5,#wordTable do
   table.insert(newTable,wordTable[i])
end

-- if you want to put the indexes after 4 (5 and above) together in a string, its way simpler

local newStr = table.concat(wordTable, " ", 5) -- 1st argument is table, 2nd is the separator (typical is " " or ", ", but can be anything, 3rd is the index to start from, default is 1, 4th is the ending array, default is the end of the array (#wordTable)
1 Like

table.concat has a parameter that lets you pick the start and end index.

game.Players.PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(msg)

local message = string.split(message, " ")

if string.lower(message[1]) == 'i' and string.lower(message[2]) == 'like' and string.lower(message[3]) == 'cake' and string.lower(message[4]) == 'because' then
	print(table.concat(message, " ", 5))
end
2 Likes

Oh dear, you’re both completely right, silly me!
You’ve saved me, please accept these gifts for your effort and happy new year! :gift: