Help with chatted teleport to targeted player

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? :scream:

image

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)
2 Likes

So, what’s the problem here?

Best regards,
Cookie

I just need to make my player teleport to the playerName .tp playerName

example
Me → i typed: .tp playerName → I have been teleported to the player I have written

This would teleport the person to the targeted player

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

Exactly, I basically need to teleport player 1 to the player 2

like this: Player 1 has been teleported to Player 2

image

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)
2 Likes

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

1 Like

I would just use HumanoidRootPart.CFrame = CFrame.new(Player2.Character.Head) to teleport the player. It works for me.

2 Likes

Thank you very much brother! this worked perfectly :smiley:

3 Likes

And thank you all very much for your help. :smiley:

3 Likes