Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I go about making my follow script detect the model closest to them?

The title explains my problem.

if string.find(script.Parent.Name, "Blue") then
	EnemyTeam = "Red"
else
	EnemyTeam = "Blue"
end

function findTarget()
	for _, v in pairs(game.Workspace:GetChildren()) do
		if v:IsA("Model") and string.find(v.Name, EnemyTeam) and v:FindFirstChild("Humanoid") and v ~= script.Parent and v.Humanoid.Health > 0 and not game.Players:GetPlayerFromCharacter(v.Parent) then
			script.Parent.Humanoid:MoveTo(v.Torso.Position)
		end
 	end
end

while true do
	wait(.2)
	findTarget()
end

1 answer

✓ Accepted answer

You can use Magnitude to find the distance between 2 Vector3 points. The easiest way to do so is like so:

(Part1.Position - Part2.Position).Magnitude

My suggestion is to create a function that loops through every model, and compares the position of the model's primarypart to your humanoidrootpart, and once it finds the thing that's closest it returns it.

1 comment

0

Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.