One of the ways I can think of, is comparing magnitudes, and checking which one is the closest, ask smallest.
You could for e.g, calculate the magnitude between your character’s head and someone’s else head, and see if that’s the smallest magnitude [relatively to other players] and if it is, tp you/him to him/you.
local players = game:GetService("Players")
local tool = script.Parent
tool.Activated:Connect(function()
if not tool.Enabled then return end
tool.Enabled = false
local character = tool.Parent
local player = players:GetPlayerFromCharacter(character)
if player then
local closestDistance, closestHumanoid = math.huge, nil
for _, humanoid in ipairs(workspace:GetDescendants()) do
if humanoid:IsA("Humanoid") then
local humanModel = humanoid:FindFirstAncestorOfClass("Model")
if humanModel then
if humanModel.PrimaryPart then
local distance = player:DistanceFromCharacter(humanModel.PrimaryPart.Position)
if distance < closestDistance then
closestDistance = distance
closestHumanoid = humanModel
end
end
end
end
end
if closestHumanoid then
character:PivotTo(closestHumanoid:GetPivot())
end
end
task.wait(3)
tool.Enabled = true
end)
Change math.huge with whatever distance the range should be.
if you just want to go to the closest player humanoid i suggest just looping though players and their characters but if u wanna do it to all humanoids u could make a region3 the size of ur radius and loop through the parts in the region for a humanoid and check the magnitude so u dont loop through the entire workspace