Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you detect wether a player chatted a certain word?

Asked by 6 years ago

So, I've been trying to do chat commands and stuff, but when you say the command word in a sentence it doesn't activate. I THINK you have to use :match() for this? I don't know, so could anyone help me out? Here's the test code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
player.Chatted:Connect(function(message)
    if message:lower() == "lumos" then
        game.ReplicatedStorage.LumosEvent:FireServer(mouse.Hit.p)
    end
end)
0
The code itself works fine, I just don't know how to detect the command word in a statement bearpro2 6 — 6y

2 answers

Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
6 years ago

You can use string.sub,string.match, or string.find, most likely string.match or string.find

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
player.Chatted:Connect(function(message)
       if (message:lower()):match('lumos') then
              game.ReplicatedStorage.LumosEvent:FireServer(mouse.Hit.p)
       end
end)
Ad
Log in to vote
0
Answered by
RootEntry 111
6 years ago
Edited 6 years ago

You can use the message argument.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

player.Chatted:connect(function(msg)
            if msg = string.lower("lumos") then
            game:GetService("ReplicatedStorage").LumosEvent:FireServer(mouse.Hit.p)
        end
end)

Answer this question