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)
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
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!
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.
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.