hi im ctg i need help again
function tower.FindTarget(newTower, range, mode)
local bestTarget = nil
local bestWaypoint = nil
local bestDistance = nil
local bestHealth = nil
for i, mob in ipairs(workspace.Mobs:GetChildren()) do
local distanceToMob = (mob.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
local distanceToWaypoint = (mob.HumanoidRootPart.Position - workspace.Map:FindFirstChildOfClass("Folder").Waypoints[mob.MovingTo.Value].Position).Magnitude
if distanceToMob <= range then
if mode == "Near" then
range = distanceToMob
bestTarget = mob
elseif mode == "First" then
if not bestWaypoint or mob:WaitForChild("MovingTo").Value >= bestWaypoint then
if not bestWaypoint or mob:WaitForChild("MovingTo").Value > bestWaypoint then
bestDistance = nil
end
bestWaypoint = mob.MovingTo.Value
if not bestDistance or distanceToWaypoint < bestDistance then
bestDistance = distanceToWaypoint
bestTarget = mob
end
end
elseif mob == "Last" then
if not bestWaypoint or mob.MovingTo.Value <= bestWaypoint then
bestWaypoint = mob.MovingTo.Value
if not bestDistance or distanceToWaypoint > bestDistance then
bestDistance = distanceToWaypoint
bestTarget = mob
end
end
elseif mode == "Strong" then
if not bestHealth or mob.Config.Health.Value > bestHealth then
bestHealth = mob.Config.Health.Value
bestTarget = mob
end
elseif mode == "Weak" then
if not bestHealth or mob.Config.Health.Value < bestHealth then
bestHealth = mob.Config.Health.Value
bestTarget = mob
end
end
end
return bestTarget
end
end
here is the target system but when in first target it will attack first enemy but when another enemy go first it still shoot the last first enemy idk i dont have idea to fix this