My chat detector script is not working

Hey, so I wrote a script in which people have to chat a specific message for the game to teleport them to another place; it’s not working at all and i’m not getting any errors

here’s the script i wrote:

local players = game:GetService("Players")

local exitCommand = "We can't let you leave"

function onChatted(Chat, recipient, speaker)
	local name = speaker.Name
	local chat = string.lower(Chat)
	
	if (chat == exitCommand) then
		local teleportService = game:GetService("TeleportService")
		local player = players:FindFirstChild(name)
		
		if name then
			teleportService:Teleport(7693303137, name)
		end
	end
end

function onPlayerEntered(Player)
	Player.Chatted:Connect(function(chat, recipient) onChatted(chat, recipient, Player) end)
end

game.Players.PlayerAdded:Connect(onPlayerEntered)

any ideas as to why it’s not working? did i use the wrong services, or perhaps made a mistake in my code or added unnecessary lines?
any help is appreciated, thank you in advance

1 Like

You shouldn’t put an onChatted in a PlayerAdded function, it doesn’t work I think. Put this in a LocalScript and remove any part about PlayerAdded, and make player a variable.

teleportService:TeleportAsync(7693303137, speaker)

Really not sure why you’re doing some of the stuff you’re doing.

What he did was right.
You can do that, it works

30xharsss

1 Like

You should always put the PLAYER as the first variable btw (as it will theoretically return it anyway as the player)

1 Like

They’re trying to teleport once a chat command is run, or something is said. Not sure why this is set up how it is though.

Roblox is currently down, so services may not work.

I know exactly what they’re trying to achieve, I’m just not sure why they are doing stuff like getting a player’s name just to find the player instance matching that name.

1 Like

There are two issues here. You aren’t seeing any errors for the second issue because that code doesn’t get reached. Your script does exactly what you wrote it out to do.

First issue is that you’re converting the player’s chat message to lowercase but the string assigned to exitCommand is not full lowercase. The capital W on We makes the comparison untrue. This is what your comparison is being evaluated as:

local exitCommand = "We can't let you leave"
local myChat = "We CAN'T let you LEAVE"

print(string.lower(myChat) == exitCommand) --> false
-- we can't let you leave =/= [W]e can't let you leave

If you’re changing the player’s chat message to lowercase then likewise you need to convert the check case to lowercase or just write it out in lowercase to begin with.

local exitCommand = "We can't let you leave"
local myChat = "We CAN'T let you LEAVE"

print(string.lower(myChat) == string.lower(exitCommand)) --> true

-- So this is your if statement
if string.lower(myChat) == string.lower(exitCommand) then

Second issue, which would produce an error, is that you’re passing the player’s name to Teleport. If you read the documentation, Teleport requires a player instance not a name. Teleport would not be able to cast a string when expecting an object.

TeleportService:Teleport(UserId, Player)

Fix those and your code is fine, other than a few small practice issues (for example, you’re getting the player twice for some reason… you pass them to onChatted and then make a variable to get them again unnecessarily, just use the player instance you already had).

Aside from that, my personal recommendation is that you do any kind of chat commands via the Lua Chat System – see Command Functions for more information. Chatted is a legacy event, I recommend that for new work you use the LCS API if you want to extend chatting functionality. It’s super nice to use too and includes mostly all you’d want!

Here’s some rough code for a command function:

local TeleportService = game:GetService("TeleportService")

local EXIT_COMMAND = "we can't let you leave"
local EXIT_PLACE_ID = 7693303137

-- For standalone scripts, ChatService can be retrieved by requiring the
-- module under ServerScriptService.ChatServiceRunner.
local function Run(ChatService)
    local function secretChatTeleport(speakerName, message, channelName)
        local speaker = ChatService:GetSpeaker(speakerName)
        local player = speaker:GetPlayer()

        -- We don't care if a non-player speaker says this
        if not player then return false end

        -- EXIT_COMMAND is predictably lowercase but message isn't
        if string.lower(message) == EXIT_COMMAND then
            TeleportService:Teleport(EXIT_PLACE_ID, player)
            return true
        end

        -- Wrong command, continue processing message
        return false
    end

    ChatService:RegisterProcessCommandsFunction("secret_chat_teleport", secretChatTeleport)
end

return Run
3 Likes

would i have to put this in serverscriptservice or somewhere in startercharacter/starterplayer or even chat? i still suck at chat function stuff so i’m not sure where i have to put this, i had the old code in serverscriptservice

i’m guessing i’d have to use that instead of

teleportService:Teleport(7693303137, name)

another question i have is where this script would go, would it be a localscript in starterplayer/character, or a server script in serverscriptservice? i still am relatively bad at chat function stuff so it’d be useful to know

I think using SpeakerAdded would work instead on plr added