How do I split a sentence?

Hi, I’m making a server message thing, and I want to display the message when a player says ;servermessage (a sentence or a string), but I don’t know how I can do this, here’s the script;

local command = ";servermessage"

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(msg)
		
		local Split = msg:split(" ")
		
		if Split[1] == command then
			
		end
		
	end)
end)

Help is appreciated, thanks.

You can use string.sub to get the first few characters of a string. Example:

local text = ";servermessage"
local textLength = string.len(text)

if string.sub(chatMessage, 1, textLength) == text then
    ...
end
1 Like

You can use string patterns, along with string:match.

Allow me to briefly explain to you how you could use string:match before I let you take a look at the wiki.

Basically, string patterns have multiple use. For example:

  • . - All characters
  • %a - Letters
  • %c - Control Characters
  • %d - Digits (Like for finding dates like DD-MM-YYYY which would then become %d%d-%d%d-%d%d%d%d)
  • %l - lowercase characters
  • %p - punctuation characters
  • %s - space characters
  • %u - uppercase characters
  • %w alphanumeric characters
  • %x - hexadecimal digits
  • %z - character with representation 0

There are also some characters that are called magic characters:

( ) . % + - * ? [ ^ $

A char-set also allows you to create your own character classes. For example, the char-set [%w+] gets the longest sequence of numbers until something else is reached, like a space character.

local text = "hello 12345678910 hello"
local numbers = string.match(text, "^hello (%w+)$"
print(numbers)

The output would then be

12345678910

So for your situation,

game.Players.PlayerAdded:Connect(function(Player)
    if Player.Playerid == game.CreatorId then --restrict this only to the owner of the game
        Player.PlayerChatted:Connect(function(msg)
        local command, sentence = string.match(msg, "^(%a;) (.+)$")
        if command ~= nil and sentence ~= nil then
            if command == ";servermessage" then
                --your code here, the sentence is the sentence specified
            end
        end
    end
end)

I’d also like to mention that both $ and ^ are anchors. ^ marks the start of the string, $ marks the end of the string. So you can select a specific segment of a string with this.

Although, that’s one of the methods. One of the simpler ways to do this would be to:

local serverMessageCommand = ";servermessage"

game.Players.PlayerAdded:Connect(function(Player)
    if Player.Id = game.CreatorId then --restrict this command only to the game owner
        Player.Chatted:Connect(function(msg)
        if msg:sub(1, serverMessageCommand:len()):lower() == ";servermessage" then
            local sentence = msg:sub(serverMessageCommand:len() + 2)
            if sentence ~= nil then
                --your code here
            end
        end
    end
end

That’s all there is to it. If there are any errors, please post them here. They are also helpful in finding out what’s wrong. Also, check out the string pattern wiki here.

Additional Info

string.len()

The method :len() or string.len() returns the length of a string in numbers. Let’s say the text is “text”, it would then return 4. Space characters are considered and included in a string, so don’t forget about that!

For more information, you can check out more about strings in this link.

5 Likes

Thank you so much for detailing that much on a small post. :slightly_smiling_face:

1 Like

No problem! I’m willing to help anyone in need in scripting. I was once just like you, and not much people helped me, so I’m willing to change that and let other beginner developers start freshly and have a bright future. :wink:

4 Likes