My Teleport Script Always Teleports You When You Say Something and I Don't Want It To Do That

Viewed 22

Every time I say a single word or letter it will always teleport me even though I don't want it to. I just want me to teleport me when I say what I put in it.

Here is the code:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(raw_msg)
        local msg = raw_msg:lower()
    
        if msg == ":cmds" or ";cmds" or "/cmds" or ":cmd" or ";cmd" or "/cmd" or 
":commands" or ";commands" or "/commands" or ":command" or ";command" or "/command" 
then
            local char = player.Character
            local humanroot = char:WaitForChild("HumanoidRootPart")
        
            humanroot.Position = game.Workspace["Secret room 
1"]:WaitForChild("Punish").Position + Vector3.new(0, 1, 0)
        end
    end)
end)
1 Answers
if msg == ":cmds" or ";cmds" or "/cmds" or ":cmd" or ";cmd" or "/cmd" or ":commands" or ";commands" or "/commands" or ":command" or ";command" or "/command"

This is not the correct way to test if msg is equal to any one of these multiple values. The strings themselves are not nil or false so they evaluate to true.

Try the following:

if msg == ":cmds" or msg == ";cmds" or msg == "/cmds" or msg == ":cmd" or msg == ";cmd" or msg == "/cmd" or msg == ":commands" or msg == ";commands" or msg == "/commands" or msg == ":command" or msg == ";command" or msg == "/command" then
    -- Code
end

However, this can be simplified:

if ({["/"] = true, [":"] = true, [";"] = true})[msg.sub(1, 1)] and ({["cmd"] = true, ["cmds"] = true, ["command"] = true, ["commands"] = true})[msg.sub(2, -1)] then
    -- Code
end
Related