How would I check if a player types a certain message in the chat?

Player typed certain message in chat function? How would I do it?

I want to make it so when a player types in chat the system checks if its a certain message (Example: /remove) and then there is space for me to code a function

3 Likes
game.Players.PlayerAdded:Connect(function(Player)
    Player.Chatted:Connect(function(Chat)
        print(Chat)
    end)
end)

or if your using local script stuff:

local Player = game.Players.LocalPlayer

Player.Chatted:Connect(function(Chat)
    print(Chat)
end)
6 Likes

But how do i check if the message chatted is a certain message (Example: /remove)

This is the wrong category. Please go to #help-and-feedback:scripting-support

1 Like
game.Players.PlayerAdded:Connect(function(Player)
    Player.Chatted:Connect(function(Chat)
        if Chat == "/remove" then
            print("Removing Thing")
        end
    end)
end)
7 Likes

@ScriptLocalize not working neither printing any errors to the output…

1 Like

what are you using? local script thats in the player or server script?

1 Like

I am using a Local Script in the workspace

i think local scripts only works if its in the player…

i mean like “StarterGui”, etc.
try to use server script

1 Like

Localscripts doesn’t work inside Workspace unless they are parented by a player character.

1 Like

still does not work, i will try putting it in the player

ofc it will not…

try this on the local script

local Player = game.Players.LocalPlayer

Player.Chatted:Connect(function(Chat)
    if Chat == "/remove" then
        print("Removing Thing")
    end
end)
1 Like

where do i put the local script, workspace

StarterPlayer.StarterPlayerScripts

try to put it in the StarterGui

A LocalScript isn’t the right thing to use in this scenario since you want everyone else to see what you’re doing. You should make it a Script in ServerScriptService instead(it’s bad practice to put Scripts in the Workspace since it’s disorganized).

Edit: Also, if you want to make commands I recommend you follow this tutorial instead of using the Chatted event.

Hello, you would probably want to do it server sided, so you can replicate it to all clients at once. For that you have to make a Script in ServerScriptService and the code should be like:

game.Players.PlayerAdded:Connect(function(player)
  player.Chatted:Connect(function(msg)
        if string.sub(msg, 1, 8) == "/remove " then
              local arguement = string.sub(msg, 9) --If you want to get the string in chat after your prefix
        end
  end)
end)

You can set the listener through a LocalScript which will only run once it’s replicated to a client, so it’s the same thing in functionality as far as the OP is concerned - the OP only requires a method to check for a certain phrase in a message sent from a player and that’s been answered a few times here already.

-- pseudo, get the player either through the client or server through the Players.PlayerAdded event.
local function onChatted(message)
    if message:match("/remove") then
    --// if it includes "/remove" anywhere in the message.. use string.sub or expand on the current function to ensure the phrase is at the beginning
    --// do something
    end
end
player.Chatted:Connect(onChatted)
1 Like

try to put the localscript in the StarterPlayer > StarterPlayerScripts, because these scripts are shored in the PlayerScripts, and they don’t require a playeradded event since they get added after (not sure) the player, so you can try this

local player = game:GetService("Players").LocalPlayer
local command = "/remove" -- there is a better way to do this, but for the sake of this post, im doing it the easy way

player.Chatted:Connect(function(message) -- when the player chats
    if message:lower():sub(1,#message) == command:lower() then -- this allows you to run the command even if you dont put the right capitilization, like if you say /RemOve, it will still work,
        -- Your code to do something here
    end
end)

also suggest you use a remote event to do stuff, or use the chatted event in SSS

Still nothing happening, I have no idea why this is happening.