How to get all the words after one in a message roblox (string.sub())

Hello developers, in this short tutorial I will explain how to get all the words in a message after a specific one as i saw that it’s a thing that has been asked a lot of times.

What we are going to use to make this is:

  1. A chat message
  2. String.sub

We will use the player.Chatted event to decect when a player sends a message

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        local cuttedMessage = string.sub(message, number of characters you want to cut until the first word of the part of the message you want, #message)
    end)
end)

So with the code above we remove all the characters of the message before the word of the message’s part we want to get.

example:

message = "Hello this is a test message"

let’s say that we want to get the part “test message”, the character before the “t” of “test” are 16, you have to include also the spaces, with string.sub(message, 16) we say to the code to get the string (message) and then go ahead from the first character by 16 characters the 16th character is actually the space before the “t” of “test”.

This could be util if you are making a kick/ban system using the roblox chat.

Let’s say that you are making a kick/ban system and you have other arguments before the part of the message that could be the reason, everything you need to do is get the length of the arguments before the interested part and that the command is “/kick”

local playerToKick = -- the part of the string that contains the player's name

local reason = string.sub(message, 5 + #playerToKick + 2)

So basically in the little code above the number 5 is the lenght of “/kick” in characters, the #playerToKick is the lenght of the player to kick name in characters and the 2 is the number of spaces contained in the message before the reason part that in this case are 2 (the one between “/kick” and the one between the player’s to kick name and the first character of the reason)

You can learn more about strings here.

Hope that this little tutorial helps you! Feedbacks are welcome!

4 Likes

Title is very, well, it’s not what I expected. I thought you were gonna use string.Split(“ “). Anyways good tutorial

3 Likes

Actually i didn’t use it because the goal wasn’t to get the arguments but every word after a certain one like to make a reason for a kick/ban system, so i just used string.sub to skip all the characters before the specific word.

2 Likes

Yeah I later understood when I read the post, just thought you were doing something else.

1 Like

Ok, i will modify the title a bit just to evitate misunderstanings

1 Like