Okay so i’ve been working on my game and i used gnomecode tower defense series but i found a bug in the “Target first mob” part and i don’t know how to fix it
this is the whole function code:
function tower.FindTarget(newTower, range, mode)
local bestTarget = nil
local bestWaypoint = nil
local bestDistance = nil
local bestHealth = nil
local map = workspace.Map:FindFirstChildOfClass("Folder")
for i, mob in ipairs(workspace.Mobs:GetChildren()) do
local distanceToMob = (mob.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
--local distanceToWaypoint = (mob.HumanoidRootPart.Position - map.Waypoints[mob.MovingTo.Value].Position).Magnitude
local distanceToWaypoint = (mob:WaitForChild("HumanoidRootPart").Position - map.Waypoints[mob.MovingTo.Value].Position).Magnitude
if newTower:GetAttribute("NoAttack") then continue end
local isHidden = mob:FindFirstChild("IsHidden")
local seeHidden = newTower:FindFirstChild("Config"):FindFirstChild("SeeHidden")
if not isHidden and not seeHidden or isHidden and seeHidden or not isHidden and seeHidden then
if distanceToMob <= range then
if mode == "Near" then
range = distanceToMob
bestTarget = mob
elseif mode == "First" then
if not bestWaypoint or mob.MovingTo.Value >= bestWaypoint then
if not bestWaypoint or mob.MovingTo.Value >= bestWaypoint then
if not bestWaypoint or mob.MovingTo.Value > bestWaypoint then
bestDistance = nil
end
bestWaypoint = mob.MovingTo.Value
if not bestDistance or distanceToWaypoint < bestDistance then
bestDistance = distanceToWaypoint
bestTarget = mob
end
end
end
if mob.MovingTo.Value == bestWaypoint and (not bestDistance or distanceToWaypoint < bestDistance) then
bestDistance = distanceToWaypoint
bestTarget = mob
end
elseif mode == "Last" then
if not bestWaypoint or mob.MovingTo.Value < bestWaypoint then
bestWaypoint = mob.MovingTo.Value
bestDistance = nil
end
if mob.MovingTo.Value == bestWaypoint and (not bestDistance or distanceToWaypoint > bestDistance) then
bestDistance = distanceToWaypoint
bestTarget = mob
end
elseif mode == "Strong" then
if not bestHealth or mob.Humanoid.Health > bestHealth then
bestHealth = mob.Humanoid.Health
bestTarget = mob
end
elseif mode == "Weak" then
if not bestHealth or mob.Humanoid.Health < bestHealth then
bestHealth = mob.Humanoid.Health
bestTarget = mob
end
end
end
end
end
return bestTarget
end
and this is where i found the bug:
elseif mode == "First" then
if not bestWaypoint or mob.MovingTo.Value >= bestWaypoint then
if not bestWaypoint or mob.MovingTo.Value >= bestWaypoint then
if not bestWaypoint or mob.MovingTo.Value > bestWaypoint then
bestDistance = nil
end
bestWaypoint = mob.MovingTo.Value
if not bestDistance or distanceToWaypoint < bestDistance then
bestDistance = distanceToWaypoint
bestTarget = mob
end
end
end
if mob.MovingTo.Value == bestWaypoint and (not bestDistance or distanceToWaypoint < bestDistance) then
bestDistance = distanceToWaypoint
bestTarget = mob
end
The thing is, i tried removing an unnecesary check for bestwaypoint but when i do that my towers attack randomly and i want them to attack the first mob.
The problem is that if i dont fix this, only some towers will attack the mobs and others will just do nothing
i’ve also tried replacing that code with one more efficient and short
elseif mode == "First" then
if not bestWaypoint or mob.MovingTo.Value > bestWaypoint then
bestWaypoint = mob.MovingTo.Value
bestDistance = nil
end
if mob.MovingTo.Value == bestWaypoint and (not bestDistance or distanceToWaypoint < bestDistance) then
bestDistance = distanceToWaypoint
bestTarget = mob
end
but i still have the same problem, and i think this last one makes the problem more noticeable