How to stop all Tweens in a table?

I am trying to solve a pathfinding script, but I am trying to troubleshoot it by using a simpler code.

-- Function to move a unit to a specific position
local function MoveUnitToPosition(unit, position)
	local goal1 = {}
	goal1.Position = Vector3.new(9.5, 0.5, 8.5) -- First position to go through
	local tweenInfo1 = TweenInfo.new(TWEEN_DURATION, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	local tween1 = TS:Create(unit, tweenInfo1, goal1)

	local goal2 = {}
	goal2.Position = position -- Next position after passing through (9.5, 0.5, 8.5)
	local tweenInfo2 = TweenInfo.new(TWEEN_DURATION, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	local tween2 = TS:Create(unit, tweenInfo2, goal2)

	-- Chain tweens together
	tween1.Completed:Connect(function()
		tween2:Play()
	end)

	-- Play the first tween to start the sequence
	tween1:Play()

	-- Store tweens in activeTweens table
	activeTweens[unit] = { tween1, tween2 }
end

-- Function to stop the tween for a unit
local function StopUnitTween(unit)
	--print(activeTweens)
	if activeTweens[unit] then
		-- Cancel the tween
		print(activeTweens)
		for _, tween in ipairs(activeTweens[unit]) do
			--print(tween)
			tween:Pause()
			tween = nil
		end
		activeTweens[unit] = nil
	end
end

StopUnitTween(unit) is supposed to stop all tweens to a given unit. In activeTweens, it contains all the parts that are tweening. For each part, there are Tweens, which translate to points for the part to go to.

For example,
Part 1 & Part 2 will go to Point A, and then Point B. So it will tween from its current position to Point A and then Point B.

For this scenario, the parts will be given a maximum of 2 points and the first point, Point A, will stay constant and remain for every new input of Tween instructions given.

However, when I run the code, I let the parts move a bit before arriving at Point A and run StopUnitTween(unit) to stop the movement. When I give a new input of tweens, it will ignore Point A and instead go to the next point. This problem doesn’t happen when the parts arrive and leave Point A.

This causes an issue when there are more than 2 points for the part to tween to (i.e Pathfinding) and it will “skip” the in-between points from its origin to the final destination. The stop function serves as to: correct the part when its in the middle of its journey and is given a new destination, so it no longer moves on the old series of points, and to stop the part from moving altogether.

Have you considered doing some vector math to calculate the direction instead of tweens?

You can form a unit vector by calculating the difference between current point and next point, then you can check the magnitude of the object from the next point, if its less than 5, move to the next point and resolve the unit vector. If you want to apply the unit vector simply add the unit to the current position every frame. This allows the part to move based on physics steps and makes it more smooth and less memory heavy than tweens.

Now, if you want to make the object look in the direction of the part you can solve for the dot product of the two vectors and form a projection vector based on the composite of the current vector and move to vector.

Sorry, I’m not so literate in vector math for ROBLOX programming. I would assume that the “movement” from each point to another would be in a loop for the vector math to work?

No worries, simply just do

( (goal - current) / (goal - current).Magnitude ) + myPosition

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