Ok so im making a tower defense game movement system and I am having a couple issues using TweenService
.
Here are my issues:
-
Model’s parts not following the PrimaryPart, even if the model is Anchored and all of its parts are welded to the PrimaryPart;
-
After about 50 zombies spawn, the Tweens for the other spawned zombies completely stop working and they tween to the wrong waypoint.
I recently switched to TweenService
over :Lerp
because someone suggested it. My lerp system worked fine but I couldn’t figure out making the zombie look smoothly to the next waypoint.
Anyways, here is the script:
function Move_Enemy(enemy, enemyID)
for x, node in pairs(workspace.Path.Nodes:GetChildren()) do
local enemyModel = workspace.Enemies[enemyID]
local pathOffset = workspace.Enemies[enemyID]:GetAttribute("PathOffset")
local nodeAttribute = workspace.Enemies[enemyID]:GetAttribute("Node")
local pathZOffset = nil
if node:GetAttribute("Direction") == "Left" then pathZOffset = -pathOffset else pathZOffset = pathOffset end
local previousNode = workspace.Path.Nodes:FindFirstChild(tostring(x - 1))
if not previousNode then previousNode = workspace.Path.StartNode end
local nextNode = workspace.Path.Nodes:FindFirstChild(tostring(x + 1))
local nodeOffsetCFrame = node.CFrame * CFrame.new(pathOffset, enemy.HipHeight, pathZOffset)
local pnoc = previousNode.CFrame * CFrame.new(pathOffset, enemy.HipHeight, pathZOffset)
local distance = (Vector3.new(nodeOffsetCFrame.X, nodeOffsetCFrame.Y, nodeOffsetCFrame.Z) - Vector3.new(pnoc.X, pnoc.Y, pnoc.Z)).Magnitude
local tweenTime = distance / enemy.Speed
local tweenInfo = TweenInfo.new(tweenTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
local movementTween = TweenService:Create(enemyModel.PrimaryPart, tweenInfo, {CFrame = nodeOffsetCFrame})
movementTween:Play()
movementTween.Completed:Wait()
movementTween:Destroy()
if nextNode then
local nnoc = nextNode.CFrame * CFrame.new(pathOffset, 1, pathZOffset)
--workspace.Enemies[enemyID]:PivotTo(CFrame.lookAt(workspace.Enemies[enemyID].HumanoidRootPart.Position, Vector3.new(nnoc.X, nnoc.Y, nnoc.Z)))
else
enemyModel:Destroy()
table.remove(Database, enemyID)
end
end
end
And I want to know if I should move out to :Lerp
again.
Thank you for standing by.
DISCLAIMER: Only recommend stuff for me or tell me whats wrong with my script! I do not want whole scripts.