I’m currently working on a tower defense game and I’m using tweenservice to move the enemies. However, whenever the enemies die the tween still continues until it reaches the next waypoint. Is there a better way to move the enemies?
Code:
function move(mobtomove,mobhumanoid,speed)
local reachedend = false
local node = path
while reachedend == false do
if mobtomove.Parent and mobhumanoid.Health > 0 then
local nextnode = node:GetChildren()
if (#nextnode > 0) then
node = nextnode[1]
local pnode = node.Parent
mobtomove.PrimaryPart.Orientation = pnode.Orientation
local movetween = tweenservice:Create(mobtomove.PrimaryPart,TweenInfo.new((pnode.Position-node.Position).Magnitude/speed,Enum.EasingStyle.Linear),{Position = node.Position})
movetween:Play()
movetween.Completed:Wait()
else
reachedend = true
end
else
break
end
end
if mobtomove.Parent and mobhumanoid.Health > 0 then
base.CurrentHealth.Value -= mobhumanoid.Health
warn(mobtomove.Name,"Has reached the end, mob health left:",mobhumanoid.Health,"base health left:",base.CurrentHealth.Value)
mobtomove:Destroy()
end
end
If I use pathfinding and the enemy speed is too high they will just fly off the track which is why tween service is good but also bad. Same goes to @vf9r.
Why not just use collision groups to avoid this scenario? Or have you already tried this? I’m assuming by flying off the track you mean when the enemy attacks? I’m not really sure what you mean.
So a tower defense game consists of your towers and enemies trying to invade your base by walking along a set path. Your main objective is to use towers to kill your enemies. The reason im using tweenservice is because tweenservice makes it so if the enemy is going too fast they cant just fly off the course. If I use pathfinding service or humanoid:moveto and the humanoid is going superfast the movements will look messy as they will literally be flying around the map trying to reach the checkpoints.
I’m using normal pathfinding, but I applied a part onto the players head, and welded it to the player, this stopped the player from flying off the edge as they do with being scaled down.