✓ Accepted answer
There are two ways to use string.sub:
string.sub(String, Beginning, End)
String:sub(Beginning, End)
Both these ways work fine. To use it in your script, you can do it like this:
game.Players.LocalPlayer.Chatted:connect(message)
if message:sub(1,3) == "abc" then
end
end)
Or like this:
game.Players.LocalPlayer.Chatted:connect(message)
if string.sub(message,1,3) == "abc" then
end
end)
NOTE: if you type "ABC", the if will return false because "ABC" is not the same as "abc". In order to detect uppercased messages, you'll have to make the string all lowercase, by using the string.lower(String) function
EDIT: I answered your comment
game.Players.LocalPlayer.Chatted:connect(message)
local Msg = string.lower(message)
local P1 = string.find(Msg, "(")
local P2 = string.find(Msg, ")")
if P1 and P2 then
local InMsg = string.sub(Msg, P1 + 1, P2 - 1)
if InMsg == "abc" then
end
end
end)
Hope this helped!
NOTE: For more info on string manipulation, click here: String Manipulation
3 comments
Thanks!
Oh, also, one more question, how could I use strings to exclude certain characters? I want to excluded "msg", "(", and ")", and then only accept the things inside of it. I am currently using a string.sub to use all characters 5 and up, but that would include the parentheses at the end of the message. How could I get around this?
I answered your comment in my question