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
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.