Tool that brings you to the closest humanoid

Ok so what Im trying to achieve is to make a tool script that will bring you to the closest humanoid in a certain range.

Here is an example from the game “Shindo Life”


Once Activated, the tool will bring the player to the closest humanoid in a certain range.

In the video, the attack is from a textbutton and not a tool, what Im trying to use it a tool. So don’t get confused

Im not asking for a script, Im simply asking how to do it.
Thanks for your time.

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.

2 Likes

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