I am trying to make something fire when a player says something that includes a persons name. For example, if they say, ":fly NoobSlayer300" then it would fire an event that makes that player fly. I am having trouble figuring out how to check if the players SAYS, ":fly" and what they type after that. How would I do that? Here is my attempt:
plr = game.Players.LocalPlayer x = ":fly", y plr.Chatted:Connect(function(msg) if string.lower(msg) == string.lower(x) and y = game.Players:FindFirstChild(y) ~= nil then print("WORKS") end
you can use string.match(string,pattern,int)
to find a specific character, word or letter in a string. Also its suggested to use the chatted event on the server, instead of the client. use string.sub(string,int,int)
to sub a string.
--server game.Players.PlayerAdded:Connect(function(player) player.Chatted:Connect(function(msg) local lower = string.lower(msg) -- turns the letters inside the string to lowercase if string.match(lower,":fly ") then local sub = string.sub(msg,6,100) print(sub) -- prints of what you could say "after the command" end end) end)
if I chat ":fly zValerian" then it'll print "zValerian" in the output.
When parsing a string, it's important to define some rules in how your text is structured. In your case, you have a command prefix (what comes before the command), a command operator (the type of command), and a command operand (the arguments of the command) -- in that order. The command prefix is static, so it's pretty easy to check for that case:
local command = ":fly person" -- if input starts with the command prefix, then... if (command:sub(1, 1) == ":") then ... end
You know that the characters after the prefix will be the type of the command, separated by a space, then followed by the operand (the effected player's name). Let's look for the type of the command first.
local command = ":fly person" -- if input starts with the command prefix, then... if (command:sub(1, 1) == ":") then -- find the characters that come after the prefix, and before -- the separating space character. local commandType = command:sub(2, "%S+") -- if the command type is "fly", then... if (commandType == "fly") then ... end end
In the code snippet above, I'm using the character class %S
to represent anything that's NOT a space character. The +
quantifier just tells the program that I want all characters that aren't a space character, up until the first space character it finds.
The next sequence is similar; I will use the same string pattern as I did before, but I will use it only on the end of the string. This can be achieved by using the $
character, also classified as a magic character, that will apply the string pattern at the end of the string.
local command = ":fly person" -- if input starts with the command prefix if (command:sub(1, 1) == ":") then -- find the characters that come after the prefix, and before -- the separating space character. local commandType = command:sub(2, "%S+") -- search for the command argument starting from the end of the string local commandArgument = command:match("%S+$") -- if the command type is "fly", then... if (commandType == "fly") then -- do some method on the commandArgument (the player) that makes them fly doSomeMethod(commandArgument) end end
I'll edit this answer a little bit to include some useful resources where you can learn more about string patterns and methods. Until then, I hope this answer was somewhat useful. Let me know if you have any questions.