How do I teleport people in who is with in a certain range

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 wan’t to teleport myself and players who are in a certain range to somewhere else (not change game)
  2. What is the issue? Include screenshots / videos if possible!
    I’m new to scripting so idk how to script by myself. I don’t know how to reference the players inside the range so I can’t teleport them. It only teleports my character.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried searching for it but I haven’t seen any solutions yet.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Local Script:

local tele1 = game.Workspace.TeleportPart
local localPlayer = game.Players.LocalPlayer
local players = game.Players.LocalPlayer.Character
local rp = game:GetService(“ReplicatedStorage”)
local tele = rp:WaitForChild(“Teleport”)
local UIS = game:GetService(“UserInputService”)
local Player = game.Players
local cooldown = 300

local range = 60
local targetposition = tele1.Position

UIS.InputBegan:Connect(function(input,isTyping)
if isTyping then
return
elseif input.KeyCode == Enum.KeyCode.Q then

		print("you pressed Q")
	
	for _, player in pairs(game.Players:GetPlayers()) do
		print(player:DistanceFromCharacter(Vector3.new(0, 0, 0)))
		
		local Character = Player.LocalPlayer.Character  
		local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
		local dist = player:DistanceFromCharacter(Vector3.new(HumanoidRootPart.Position))
		if dist > range then
			print("within range")
			Character.HumanoidRootPart.CFrame = CFrame.new(tele1.Position)
			
			else
				print("out of range")
		
		tele:FireServer()
	end
	
end
	end

end)

tele.OnClientEvent:Connect(function()
wait(cooldown)

end)

sorry if my script sucks

You’ll need to use a RemoteEvent instance to fire the server for this as each client has network ownership over their own player’s character models. Here’s a system I wrote which works.

--LOCAL

local userInput = game:GetService("UserInputService")
local replicated = game:GetService("ReplicatedStorage")
local remote = replicated:WaitForChild("RemoteEvent")

userInput.InputBegan:Connect(function(key, processed)
	if processed then return end

	if key.KeyCode == Enum.KeyCode.E then
		remote:FireServer()
	end
end)
--SERVER

local players = game:GetService("Players")
local replicated = game:GetService("ReplicatedStorage")
local remote = replicated.RemoteEvent

remote.OnServerEvent:Connect(function(player)
	if not player then return end
	local character = player.Character
	if not character 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(otherRoot.Position)
		if distance <= 100 then
			otherCharacter:PivotTo(character:GetPivot())
		end
	end
end)

The client handles client input (as is required) and the server handles all of the calculations/teleporting. A single RemoteEvent instance is required inside the ReplicatedStorage container.

You’ll need to implement a debounce (delay), if you desire to have one & specific part teleporting if necessary.

I tried the code you gave and it did teleport players within range to me but I needed them to teleport to a part’s position. so I tried to change the code but since i dont really know how to code i think i did something wrong? when my players spawned in it DID teleport me and other players to the part’s position then I tried to check if it teleports players within range by walking away and it didnt teleport others when Im too far. Then I let player2 walk away and I when made my player 1 go near him it didnt teleport player 2 anymore it only teleported player1. Do you know how to fix this? Sorry if my explanations are so hard to understand.

ServerScript:

local players = game:GetService(“Players”)
local replicated = game:GetService(“ReplicatedStorage”)
local remote = replicated.Teleport
local tele1 = workspace.TeleportPart
local targetposition = tele1.Position

remote.OnServerEvent:Connect(function(player)
if not player then return end
local character = player.Character
if not character 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 otherhumanrp = otherCharacter:FindFirstChild("HumanoidRootPart")
	

	local distance = player:DistanceFromCharacter(otherRoot.Position)
	if distance <= 100 then
		print("within range")
		otherCharacter.HumanoidRootPart.CFrame = CFrame.new(targetposition)
		character.HumanoidRootPart.CFrame = CFrame.new(targetposition)
	end
end

end)

No idea what you mean, sorry.

The script I wrote works as intended, it teleports the characters of players to the character of any player that presses the “E” key, providing they are within 100 studs of one another.

What I mean is like if it is possible to teleport everyone within 100 studs of my character to another city for example? or something like that.