Hi, i have been trying to work on this for a while and i used the Gnomecode’s tutorial but its not very accurate, is there any to fix it?
function tower.FindTarget(towerModel)
local range = towerModel.Configuration.Range
local bestTarget = nil
local bestWaypoint = nil
local bestDistance = nil
for i, Enemies in ipairs(game.Workspace.Game.Enemies:GetChildren()) do
local distanceToMob = (Enemies.PrimaryPart.Position - towerModel.HumanoidRootPart.Position).Magnitude
local distanceToWaypoint = (Enemies.PrimaryPart.Position - workspace:FindFirstChild('Game'):FindFirstChild('Map'):FindFirstChild('Map'):FindFirstChild('Waypoints')[Enemies.nextWaypoint.Value].Position).Magnitude
if distanceToMob <= range.Value then
if not bestWaypoint or Enemies.nextWaypoint.Value >= bestWaypoint then
bestWaypoint = Enemies.nextWaypoint.Value
if not bestDistance or distanceToWaypoint < bestDistance then
bestDistance = distanceToWaypoint
bestTarget = Enemies
end
end
end
end
return bestTarget
end