So basically I am having an issue, I will send a video showing it.
As you can see when a tower is shooting and if theres a new tower it will ignore the current one and the new one will do the damage only…
Client script:
coroutine.wrap(require(script.Stalker).one)(troops[towers])
Module Script:
local module = {}
local Nodes = workspace.Waypoints
local function bestTarget(tower)
local target = nil
local bestNode = nil
local bestDistance = nil
local bestHealth = nil
local bestRandom = nil
local distanceToMob, distanceToNode
for count, enemy in ipairs(_G.entities) do
if not enemy["Dead"] then
distanceToMob = (enemy.ControlBone.Position - tower.CFrame).Magnitude
distanceToNode = enemy.TotalAlpha
if distanceToMob <= tower.Range then
if tower.Mode == "First" then
if not bestNode or enemy.WaypointIndex >= bestNode then
bestNode = enemy.WaypointIndex
if not bestDistance or distanceToNode > bestDistance then
bestDistance = distanceToNode
target = enemy
end
end
elseif tower.Mode == "Last" then
if not bestNode or enemy.WaypointIndex <= bestNode then
bestNode = enemy.WaypointIndex
if not bestDistance or distanceToNode < bestDistance then
bestDistance = distanceToNode
target = enemy
end
end
elseif tower.Mode == "Strongest" then
if not bestHealth or enemy.Health >= bestHealth then
bestHealth = enemy.Health
target = enemy
end
elseif tower.Mode == "Weakest" then
if not bestHealth or enemy.Health <= bestHealth then
bestHealth = enemy.Health
target = enemy
end
elseif tower.Mode == "Random" then
if math.random(0, math.floor((#_G.EnemyDB/2) + .5)) == math.floor((#_G.EnemyDB/2) + .5) then
bestRandom = enemy
target = enemy
elseif not bestRandom and #_G.EnemyDB == count then
bestRandom = enemy
target = enemy
end
end
end
end
end
return target
end
function module.one(tower)
print(tower)
while tower do
task.wait(tower.Cooldown)
if not tower then break end
-- workspace.Towers.Spawned[tower.TowerHash].PrimaryPart.CFrame = CFrame.new(tower.CFrame.Position, Vector3.new(workspace.Enemies.Active[target.EnemyHash].PrimaryPart.Position.X ,tower.CFrame.Position.Y, workspace.Enemies.Active[target.EnemyHash].PrimaryPart.Position.Z))
local target = coroutine.wrap(bestTarget)(tower)
print(target)
--print(target)
if target then
workspace.Towers.Spawned[tower.Index].PrimaryPart.CFrame = CFrame.new(tower.CFrame, Vector3.new(target.ControlBone.Position.X ,tower.YOffset, target.ControlBone.Position.Z))
_G.entities[target.Index]["Health"] -= tower.Damage
if _G.entities[target.Index]["Health"] <= 0 and not _G.entities[target.Index]["Dead"] then
workspace.Enemies.Active[target.Index]:Destroy()
_G.entities[target.Index]["Dead"] = true
end
else
continue
end
--tower.TotalDamage += tower:GetAttribute("Damage")
end
return
end
return module