I am creating a tower defense game where enemies spawn from a part and tween to nodes on the path until they reach the player base. The issue is, when I destroy the enemy after its health has hit 0, although its destroyed, the health of the player base still goes down as if the enemy still made its way to the base. I am not sure how I would destroy the enemy AND stop it from tweening so it does not reach the player base.
This is the tower module script
local rs = game:GetService("ReplicatedStorage")
local towerData = require(script.Parent.towerData)
local tower = {}
local enemies = workspace.Enemies
function findNearestEnemy(newTower)
local range = 20
local nearestEnemy = nil
for i, enemy in ipairs(enemies:GetChildren()) do
local distance = (enemy.Position - newTower.Position).Magnitude
if distance <= range then
nearestEnemy = enemy
end
end
return nearestEnemy
end
tower.Attack = function(newTower)
local target = findNearestEnemy(newTower)
if target then
target.Health.Value -= 3
local targetHP = target.Health.Value
if targetHP <= 0 then
target:Destroy()
end
end
task.wait(1)
tower.Attack(newTower)
end
This is the enemy movement module script
local rs = game:GetService("ReplicatedStorage")
local ts = game:GetService("TweenService")
local path = workspace.Node1
local enemyData = require(script.Parent.enemyData)
local enemy = {}
local speed = 0.5
local rotationSpeed = 0.2
local currentHealth = 200
enemy.Spawn = function()
local enemies = {rs.Enemies.Grass, rs.Enemies.Rock}
local enemy = enemies[math.random(1, #enemies)]:Clone()
enemy.CFrame = path.CFrame
enemy.Parent = workspace.Enemies
local enemyHealth = Instance.new("IntValue")
enemyHealth.Name = "Health"
enemyHealth.Parent = enemy
enemyHealth.Value = enemyData.Data[enemy.Name]["Health"]
end
enemy.Move = function(enemy)
local node = path
local enemySpeed = enemyData.Data[enemy.Name]["Speed"]
local finished = false
while not finished do
local nodes = node:GetChildren()
if #nodes > 0 then
node = nodes[math.random(1, #nodes)]
local parent = node.Parent
local distance = (parent.Position - node.Position).Magnitude
local rotation = CFrame.new(parent.Position, node.Position)
local rotateTween = ts:Create(enemy, TweenInfo.new(rotationSpeed, Enum.EasingStyle.Linear), {CFrame = rotation})
rotateTween:Play()
rotateTween.Completed:Wait()
local moveTween = ts:Create(enemy, TweenInfo.new(distance*(speed/enemySpeed), Enum.EasingStyle.Linear), {Position = node.Position})
moveTween:Play()
moveTween.Completed:Wait()
else
finished = true
currentHealth -= enemyData.Data[enemy.Name]["Health"]
rs.Events.updateScreen:FireAllClients(currentHealth)
enemy:Destroy()
end
end
end
return enemy