Hi, I need to do a chat command that teleports me to the player who i typed .tp playerName
example
Me → i typed:.tp playerName → I have been teleported to the player I have written
How could i do that in my code?
script:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
-- some code:
Player.Chatted:Connect(function(message)
-- teleport to target player
if Player.TeamColor == BrickColor.new("Lapis") then
for i,Player2 in pairs(Players:GetPlayers()) do
if message == '.tp ' .. Player2.Name then
print("The player name is ..." .. Player2.Name)
print("you've been teleported to the target player")
-- some code:
end
end
end
end)
end)
Ok so firstly, when you type “.tp PLayer2” does it print(“The player name is”) like you wrote in the code? If it does then it’ll be easy to script the teleport
Sorry, maybe I didn’t say it clearly, I meant that when you tested the game and typed .tp someone, did it print in the output “The player name is…Something” and also “You have been teleported to”. This is just for testing purposes, by which I mean to test if the code even runs when you type .tp in the chat or not
Only the name of the player is printed as a test, I have not done the teleport part yet, I do not know how to do the teleport of the player who wrote the command and that this is teleported to the player who has been named in the command
Its simple, just set the player1 primary part cframe to player2 primary part cframe
like this:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
-- some code:
Player.Chatted:Connect(function(message)
-- teleport to target player
if Player.TeamColor == BrickColor.new("Lapis") then
for i,Player2 in pairs(Players:GetPlayers()) do
if message == '.tp ' .. Player2.Name then
print("The player name is ..." .. Player2.Name)
print("you've been teleported to the target player")
Player.Character:SetPrimaryPartCFrame(Player2.Character.PrimaryPart.CFrame)
-- some code:
end
end
end
end)
end)
Ok, that’s all I needed to know, if it’s printed that then it means it is working fine. Now for the teleport script, there are other methods (which are probably more reliable) but since this one is the easiest I think it works fine:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
-- some code:
Player.Chatted:Connect(function(message)
-- teleport to target player
if Player.TeamColor == BrickColor.new("Lapis") then
for i,Player2 in pairs(Players:GetPlayers()) do
if message == '.tp ' .. Player2.Name then
print("The player name is ..." .. Player2.Name)
print("you've been teleported to the target player")
-- some code:
-- Teleport script
Player.Character:MoveTo(Player2.Character.Position)
end
end
end
end)
end)
I still haven’t tested this script out so tell me if it works. Also, you should probably turn collisions off in your game otherwise it will definitely lag with this teleport script