Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to teleport to another player?

I am making an admin script and having some trouble making this script work:

TextButton.MouseButton1Down:Connect(function()
	local txt = TextBox.Text
	local chr1 = game.Players.LocalPlayer.Character
	local chr2 = game.Workspace:FindFirstChild(txt)
	chr1.HumanoidRootPart.CFrame = chr2.HumanoidRootPart.CFrame * CFrame.new(0,2,0)
end)

2 answers

✓ Accepted answer

Well, I would suggest you to move the player on Server side and not on Local side. Also, use MoveTo property instead of changing the CFrame of HumanoidRootPart. We will also be using RemoteFunction to minimize exploitation.

Local Script :

-- Add a RemoteFunction in ReplicatedStorage

local RS = game:GetService("ReplicatedStorage")

-- Also, you didn't add variable for TextBox, so I assume you are using script.Parent method.
-- WARNING : DO NOT USE game.StarterGui... to index any of the GuiObject.
TextButton.MouseButton1Down:Connect(function()
	local plr = game.Players.LocalPlayer
    local txt = TextBox.Text
	local check, msg = RS.RemoteFunction:InvokeServer(plr.Name, txt)

	if check then -- If the function returns true
		TextBox.Text = msg -- Changes the text of the TextBox to the string returned by the function
	else -- If it returned false
		TextBox.Text = msg
end)

Server Script [In ServerScriptService] :

local RS = game:GetService("ReplicatedStorage")

function RS.RemoteFunction.OnServerInvoke(player, doubleChecker, playerTo)
	if player.Name ~= doubleChecker then return false, "Exploiter Detected!" end -- It will check whether it was the player who invoked the server and not the exploiter.

	local char = player.Character or player.CharacterAdded:Wait() -- Waits for the player's character to load
	
	if workspace:FindFirstChild(playerTo) then -- Checks if the name in the text exists in the world
		local Player = workspace:FindFirstChild(playerTo) -- Assigning the variable 
		local TeleportPostion = Player.PrimaryPart.Position

		char:MoveTo(TeleportPosition) -- Moves the player model to the another player's HumanoidRootPart
		return true, "Success!" -- Returns true
	else
		return false, "Could not find" -- Returns the functions 
	end
end

Lemme know if it helps! If you want brief explanation of something mentioned above, lemme know!

EDIT : As you told me, if you don't want any type of anti-exploits then, you can simply do the following in a single LocalScript :

TextButton.MouseButton1Down:Connect(function()
	local txt = TextBox.Text
	local myPlayer = game.Players.LocalPlayer

	if game.Players:FindFirstChild(txt) then
		local toPlayer = game.Players:FindFirstChild(txt)

		local myChar = myPlayer.Character or myPlayer.CharacterAdded:Wait()
		local playerChar = toPlayer.Character or toPlayer.CharacterAdded:Wait()

		myChar:MoveTo(playerChar.PrimaryPart.Position)
	end
end)

-- The explanations were already stated in above code, if you are confused anywhere lemme know

2 comments

0

It would be nice if you could pack this all up into one script without the remote function, i am after all making an admin gui so exploiting isn't really important.

0

Okay, but if you enconter any case of exploiting, make sure to use RemoteFunctions.

at the line:

chr1.HumanoidRootPart.CFrame = chr2.HumanoidRootPart.CFrame * CFrame.new(0,2,0)

You forgot to add CFrame.new() after the =.

TextButton.MouseButton1Down:Connect(function()
	local txt = TextBox.Text
	local chr1 = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
	local chr2 = game.Workspace:FindFirstChild(txt)
	chr1.CFrame = CFrame.new(chr2.HumanoidRootPart.CFrame) * CFrame.new(0, 2, 0)
end)

Please tell me if this doesn't work, I'm new to scripting.

1 comment

0

Filtering Enabled exists

This discussion is preserved as it was. New answers, comments, and votes are closed.