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.