Find which magnitude belongs to each person

Hello fellow developers, I’ve been developing an attack which teleports you to the nearest player using magnitude and tables. I’ve found a way to get the nearest magnitude but I have no clue how to find out which magnitude it belongs to. For example, I’m next to Dummy6, I use the attack and in the output it prints 2 studs, so the nearest magnitude is 2 studs. But how do I know who it belongs to so I can specify them in the script and only damage them. Heres my script:

script.Parent.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local hum = char.Humanoid
	local list = {}
	local root = char.HumanoidRootPart
	for i,v in pairs(workspace:GetChildren()) do
		if v:FindFirstChild("HumanoidRootPart") and v ~= char then
			local dis = (root.Position - v.HumanoidRootPart.Position).Magnitude
			if dis <60 then
				table.insert(list,dis)
				table.sort(list,function(a,b)
					return a < b
				end)
			end	
		end
	end
	print(list[1])
end)

Any help is very much appreciated, thank you!

Store the distance into a seperate array, with the index as the distance and the value as the Character. When you get your closest distance, you index your seperate array with your distance! this will give you the character.
vv Add this to your existing code

 local Distances = {}
 local list = {}

--Your distance stuff
if dis < 60 then
   Distances[dis] = Character
end
--
local ClosestCharacter = Distances[list[1]]

Another way to do this is just using a simple sort.
You can do this something like:

local Smallest = math.huge --Set as a high number so that the first condition is met
local Character

--distance would be the distance between player and object
for i,v in pairs(players) do
 if (distance < Smallest) then
  Character = v
  Smallest = distance
  end
end
print(Character)
function getclosest(Hunter) --where Hunter is the player that fired the event
	local Character = Hunter.Character
	local HRP = Character.HumanoidRootPart
	
	--get elements
 	local found = {}
	for _,v in pairs(Character:GetChildren()) do
		if v:IsA("Model") and v:FindFirstChild("HumanoidRootPart") and v~=Character then
			local distance = (HRP.Position - v.HumanoidRootPart.Position).Magnitude
			if distance < 60 then
				found[distance] = v
			end
		end
	end
	
	--sort em
	local closest = math.huge
	for i,_ in pairs(found) do
		closest = math.min(closest,i)
	end
	--return the model of the closest character that isnt themself or nil
	return found[closest]
end

How is list supposed to be anything if nothing was inserted into it, what am I doing wrong?

script.Parent.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local hum = char.Humanoid
	local list = {}
	local distances = {}
	local root = char.HumanoidRootPart
	for i,v in pairs(workspace:GetChildren()) do
		if v:FindFirstChild("HumanoidRootPart") and v ~= char then
			local dis = (root.Position - v.HumanoidRootPart.Position).Magnitude
			if dis <60 then
				table.insert(distances,dis)
				distances[dis] = v.Name
				table.sort(distances,function(a,b)
					return a < b
				end)
			end	
		end
	end
	print(distances[list[1]])
end)

Sorry im a pretty new scripter :sweat_smile:

You could just add a bunch of magnitudes of every player to a list, then call table.sort on the table and return the first value.

local dists = {1, 10, 20, 21, 50}
table.sort(dists, function(a,b) 
  return a < b
end)

local closestPlayer = dists[1]

Though a better way to do this would probably be.

local localPlayer = game:GetService("Players").LocalPlayer
local dists = game:GetService("Players"):GetPlayers()
table.sort(dists, function(a,b) 
  local aCharacter = a.Character or 1000
  local bCharacter = b.Charactor or 1000
  return localPlayer:DistanceFromCharacter(a.Character.HumanoidRootPart.CFrame.Position) < localPlayer:DistanceFromCharacter(b.Character.HumanoidRootPart.CFrame.Position)
end)

local closestCharacter = dists[1]

This is could be better simplified to look better though.

script.Parent.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local hum = char.Humanoid
	local Smallest, Enemy = math.huge, nil
	local root = char.HumanoidRootPart
	for i,v in pairs(workspace:GetChildren()) do
		if v:FindFirstChild("HumanoidRootPart") and v ~= char then
			local dis = (root.Position - v.HumanoidRootPart.Position).Magnitude
			if dis < 60 and dis < Smallest then
				Smallest = dis
				Enemy = v
			end	
		end
	end
	print(Enemy)
end)

This should work! Lmk if it doesnt

This worked perfectly fine, thanks! I’m not sure why it worked but ay its nice :sweat_smile:

No problem, happy to help. Happy scripting!