All Nearest Characters?

    1. What do you want to achieve? I wish to find all nearest Humanoid Root Parts to the local character from a server script, and then teleport them to a certain place depending on their position. Almost like bringing the place under their feet.
    1. What is the issue? I’m fairly new to scripting so there’s not much I understand.
    1. What solutions have you tried so far? I’ve tried to get the characters depending on the location of the mouse, but failure.

What I’m going for is an ability that can shift the players closest to the local player to a dimension that has already been grouped and created.

1 Like

You need to loop through the Players list, check if they are within a certain by using a Magnitude check, then if that check is less that your check value, teleport them. Studio is borked at the moment, so can’t give you any code.

2 Likes

This will basically get all players and order the list returned by their distance from the current player’s character.

local player = game.Players.LocalPlayer
local char = player.Character or workspace:WaitForChild(player.Name)


function GetNearestPlayers()
	local nearestPlayers = {}
	for _,plr in pairs(game.Players:GetPlayers()) do
		local chr = plr.Character
		if (char and char.PrimaryPart and plr ~= player) then -- just check we're not reading the current player, and the other player's character exists as well as the PrimaryPart
			local pos = chr.PrimaryPart.Position
			local dist = (char.PrimaryPart.Position-pos).Magnitude
			table.insert(nearestPlayers,
				{
					Player = plr,
					Character = chr,
					Position = pos,
					Distance = dist
				}
			)
		
		end
	end
	table.sort(nearestPlayers,function(a,b) return a.Distance < b.Distance end) -- sort by distance to from closest to furthest
	--if you wish to sort from furthest to closest - change the "<" to ">"
	return nearestPlayers
end

wait(15) -- wait for some players to spawn (since this is a local script
local nearPlayers = GetNearestPlayers() -- call the function to get the list of players by disance

local closestPlayer = nearPlayers[1].Player  -- choose the first player in the list
local furthestPlayer = nearPlayers[#nearPlayers].Player -- choose the last player in the list

4 Likes