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.