Teleporter buttons deaths

I have two buttons which teleport you to areas as the title says but if the player is quite far away from the teleport destination, the player gets instantly killed. I do not know why this happens. How can I make sure this does not happen. The code:

Portals.MouseButton1Click:Connect(function()
 game.Players.LocalPlayer.character.HumanoidRootPart.CFrame = CFrame.new(-163.25, 7.113, -20.75)
  end)
Leaderboards.MouseButton1Click:Connect(function()
	 game.Players.LocalPlayer.character.HumanoidRootPart.CFrame = CFrame.new(166.75, 7.113, -21)
	
  end)

I think it’s the fact that you’re CFraming the HumanoidRootPart so far and so quick that it’s removing the Head weld and killing the character. Have you tried MoveTo?

2 Likes

How can i do this. A code example will be helpful.

Can you provide a video for us? Maybe it will help. I don’t think that teleportation will remove the head weld, so this may not be a problem with the teleportation script. Do you have some other scripts that would possibly kill the player?

(Tbh I don’t even know what magical force is binding the player’s head and body together XD)

EDIT:

You should always absolutely not use MoveTo to teleport a character. The roblox documentations states that you should always use CFrame for teleportation.

1 Like

https://developer.roblox.com/en-us/api-reference/function/Model/MoveTo just a heads up, whenever someone tells you to try using an api you don’t know how to use yet, check the developer hub.

1 Like

I think i know, is it in a local script?
If so you need to change it to a server script, i tried that before in a local script and it also made the player die instanstly.

1 Like

Yes it is in a local script. 30 char

You need to make it a server script, I think.

2 Likes

That’s exactly what happened to me when i did it in a local script.
Use a remote event.

1 Like

So what should I do to fix this cause the teleport button is in a gui.

You should use a remote event that gets fired when the player clicks the gui button.

1 Like

Ah yes now that makes sense. If you did the teleportation in a localscript, the changes won’t replicate to the server. That’s why the player dies.

You can use a remoteEvent to solve this. A remoteEvent lets the client communicate with the server. Here’s the docs for remoteEvents.

2 Likes

My scripts are not working, can i get some help.

-- Script
 
local Players = game:GetService("Players")

local teleport = game.ReplicatedStorage:WaitForChild("Teleport")

 
local function teleport(player)
	teleport:FireClient(player)
end
local function tele(player)
	local button = player.PlayerGui.MainGui.Teleporters.Frame.Portals 
	button.MouseButton1Click:Connect(teleport)
end 

 
 
--Local Script

local function onWelcomePlayerFired()
	 game.Players.LocalPlayer.character.HumanoidRootPart.CFrame = CFrame.new(-163.25, 7.113, -20.75)
end
 
teleport.OnClientEvent:Connect(onWelcomePlayerFired)

The teleporting part is still in the local script.

1 Like

You shouldn’t use FireClient in the normal script, instead, the other way round. Fire the server in a localscript, then bind a function to the event server-side to teleport the player.

1 Like

but that is how it is in the docs by roblox on remote events an the teleporting part does not even work.

You can do this locally. You aren’t supposed to change the HumanoidRootPart. Use :SetPrimaryPartCFrame on the player’s character model.

1 Like

You can’t teleport a player on the client, its the other way around mate, and also in the local script you should define the gui button there and make it fire the server in the local script.

1 Like

You’re putting your code in the wrong place. Reverse the localscript code with the serverscript and it should work perfectly.

EDIT:
In response to @KdudeDev’s post

The docs say that you can safely teleport the player by using CFrame. No need to complicate things here bud.

1 Like