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