Issues Teleporting Players by Chat Commands

I have a problem and two suggestion to change to improve it, but I don’t know how to do it. Here is the script I am developing:

-- This Script is located in ServerScriptService.
local chatCommands = {"/tp lobby", "/tp l"}

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Chat)
		if Chat == chatCommands then
			Player.Character:MoveTo(workspace.Lobby.Spawns:FindFirstChildOfClass("SpawnLocation").Position)
		end
	end)
end)

First, the problem:

I can’t teleport. I suspect it’s because of something missing from the variable, but I’m not getting what I want to do.


Finally, the suggestions:

For example, if I wrote “/tp lob” that I had already detected from “/tp lobby”, and teleported me to the location I want. I think it’s using the lower() system, but I’m not really into the area. This reduced the code, and made it easier, since if you write an error in the chat for the function, the player is not teleported.

The other suggestion would be to remove the message when it is sent. I would like it to be a hidden command, so that the chat is not overloaded, but I don’t know how to auto-remove the message.

If you can help me, I would be very grateful. Thanks for reading!

local chatCommands = {"/tp lobby", "/tp l"}

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Chat)
		if table.find(chatCommands, Chat) then
			Player.Character:MoveTo(workspace.Lobby.Spawns:FindFirstChildOfClass("SpawnLocation").Position)
		end
	end)
end)
2 Likes
game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Chat)
		if Chat == "/tp lobby" or "/tp l" then
			Player.Character:FindFirstChild("HumanoidRootPart").Position = workspace.Lobby.Spawns:FindFirstChildOfClass("SpawnLocation").Position
		end
	end)
end)

it think that works
1 Like

I was already using this one, but I wanted to improve because I wanted to put several commands, and it would be confusing to find, but thank you very much for trying to help me, I am very grateful! :slight_smile:

The problem is you’re comparing a string with an array. It should’ve error.

You want use table.find

But there will be an performance issue (because you are basically running a for loop every message sent). You will want to use string.find to check does the string have “/tp”. Maybe a string.split beforehand if you want to have multiple parameters.

Can you do it to detect the writing even if the word is not complete?

1 Like

I can but I am not willing to show you how, I would like for you to learn it. string | Roblox Creator Documentation

String Manipulation is going to be your go to, string.lower(), string.sub() and string.find()

1 Like

I’ll see what I can do with it :slight_smile:

1 Like

I will try to learn something. Thank you so much for helping me. I am grateful for it!

1 Like

Very welcome and I really appreciate you reaching out for help and mentionning your troubleshooting methods. Happy coding!

2 Likes
local players = game:GetService("Players")

local lobby = workspace.Lobby
local spawns = lobby.Spawns
local spawnLocation = spawns.SpawnLocation

local chatCommands = {"/tp lobby"}

players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	player.Chatted:Connect(function(message)
		local commandsFound = {}
		for _, command in ipairs(chatCommands) do
			if command:match("^"..message:lower()) then
				table.insert(commandsFound, command)
			end
		end
				
		if #commandsFound == 1 then
			if commandsFound[1] == chatCommands[1] then
				character:PivotTo(spawnLocation.CFrame)
			end
		end
	end)
end)

Here you go. Capitilisation is ignored (not case-sensitive), the command can be abbreviated and the more up to date “:PivotTo()” instance method is used instead, if you need any assistance with adding other commands feel free to let me know.

I tested this & it does work.

1 Like

Thanks for trying to help me, but I already found a solution. I’m glad you want to help me!

Can you do it to detect the writing even if the word is not complete?

You asked for this though.

1 Like