[SOLVED] Issues Tweening Model

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.

1 Like

General code clean up. See if this helps or gets you any closer …

Code
function Move_Enemy(enemy, enemyID)
	local enemyModel = workspace.Enemies[enemyID]
	local pathOffset = enemyModel:GetAttribute("PathOffset")
	local nodeAttribute = enemyModel:GetAttribute("Node")
	local pathZOffset = (nodeAttribute == "Left") and -pathOffset or pathOffset

	for x, node in pairs(workspace.Path.Nodes:GetChildren()) do
		local previousNode = workspace.Path.Nodes:FindFirstChild(tostring(x - 1)) or workspace.Path.StartNode
		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 = (nodeOffsetCFrame.Position - pnoc.Position).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)
		else
			enemyModel:Destroy()
			table.remove(Database, enemyID)
		end
	end
end

1 Like

this does fix a bit of my 2nd issue, but when there is even more enemies on the ground the HRP’s just dissapear. Still doesn’t fix my first issue listed above, but thank you anyways for the recommendation.

I was hoping that would help a bit … honestly hard to test anything here, so.

ah its fine, I solved my problem

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.