How do I teleport to somewhere

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to teleport my player and other players somewhere by pressing a button/key, and I want them to teleport only if they are near me within a certain distance.

  1. What is the issue? Include screenshots / videos if possible!
    Teleport - YouTube

They teleport to the destination no matter what the range.
Also I don’t really know how to script since I just started very recently so I can easily get confused in scripting.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried searching it up in Devhub and devforums

Script:

local players = game:GetService(“Players”)
local replicated = game:GetService(“ReplicatedStorage”)
local remote = replicated.Teleport
local destination = game.Workspace.TeleportDestination

remote.OnServerEvent:Connect(function(player)
if not player then return end
local character = player.Character
if not character then return end
local root = character.PrimaryPart
if not root then return end
local humanoid = character:FindFirstChildOfClass(“Humanoid”)
if not humanoid then return end
if humanoid.Health <= 0 then return end

for _, otherPlayer in ipairs(players:GetPlayers()) do
	if not otherPlayer then continue end
	local otherCharacter = otherPlayer.Character
	if not otherCharacter then continue end
	local otherRoot = otherCharacter.PrimaryPart
	if not otherRoot then continue end
	local otherHumanoid = otherCharacter:FindFirstChildOfClass("Humanoid")
	if not otherHumanoid then continue end
	if otherHumanoid.Health <= 0 then continue end

	local distance = player:DistanceFromCharacter(root.Position)
	if distance <= 100 then
		otherCharacter:SetPrimaryPartCFrame(destination.HumanoidRootPart.CFrame*CFrame.new(0,10,0))
	end
end

end)

Local Script:

local userInput = game:GetService(“UserInputService”)

local replicated = game:GetService(“ReplicatedStorage”)

local remote = replicated:WaitForChild(“Teleport”)

userInput.InputBegan:Connect(function(key, processed)

if processed then return end

if key.KeyCode == Enum.KeyCode.Q then

remote:FireServer()

end

end)

You can’t check for input on server scripts just change the players position when the remote event gets fired

teleport.OnServerEvent:Connect(function(player) Whatever you want to do End)

Edit: I don’t know how to format stuff on mobile sorry

if you want to teleport someone only if they are near you then you can check the distance between them then if the distance is close, teleport them. Here is an example:

local maxDistance = 5 -----the maximum distance you want 
if (Player1.Character.HumanoidRootPart.Position - Player2.Character.HumanoidRootPart.Position).Magnitude < maxDistance then  ----magnitude is the distance between them, if they are close which mean if the distance between them is less that 5 then teleport them

---do your teleport here

end

here are some links Magnitude

1 Like

oops I sent the wrong scripts sorry. I’ll edit it right away

I tried this but it just says
image

1 Like

oh yea i just updated the script its supposed to be Player.Character.HumanoidRootPart.Position

1 Like

It works now. Thank you so much

2 Likes