Magnitude Problem

I’m trying to use magnitude for a single target, I want to use magnitude to detect in all directions but only get one player if detected but I don’t know how and if possible a way to detect which player is the closest but I’m mainly focusing on magnitude for a single target. Any help would make me really grateful and have a good day! ;D

2 Likes
local closest = {}
for i, obj in workspace:GetChildren() do
	if obj:FindFirstChildWhichIsA('Humanoid') then
		if #closest > 0 then
			if closest[2] < (obj.HumanoidRootPart.Positon - script.Parent.Position).Magnitude then
				closest[1] = obj
				closest[2] =  (obj.HumanoidRootPart.Positon - script.Parent.Position).Magnitude
			end
		else
			closest[1] = obj
			closest[2] =  (obj.HumanoidRootPart.Positon - script.Parent.Position).Magnitude
		end
	end
end

local closestPlayer = closest[1]

NOT BUG TESTED, BE CAREFUL

Hope this helps!

1 Like