Hey everyone! The title says it all. Everytime I try to loop a tween, I can’t cancel or pause it because it will mess up with everything, like in this video:
Yet if I do never pause or cancel the tweents it works just fine.
I don’t know what to do… Here’s the code:
local fog01 = script.Parent.fog01
local fog02 = script.Parent.fog02
local TS = game:GetService("TweenService")
local menu_bool = script.Parent.Parent.Parent.Parent.Stages.Menu
local f1pos = Vector3.new(71.436, 72.561, 260)
local f2pos = Vector3.new(1.272, 72.561, 260)
local f3pos = Vector3.new(141.599, 72.561, 260)
local speed = 10
local MOVE01 = TS:Create(fog01, TweenInfo.new(speed, Enum.EasingStyle.Linear), {Position = f3pos})
local MOVE02 = TS:Create(fog02, TweenInfo.new(speed, Enum.EasingStyle.Linear), {Position = f1pos})
local MOVE03 = TS:Create(fog01, TweenInfo.new(speed, Enum.EasingStyle.Linear), {Position = f1pos})
local MOVE04 = TS:Create(fog02, TweenInfo.new(speed, Enum.EasingStyle.Linear), {Position = f3pos})
menu_bool.Changed:Connect(function()
if menu_bool.Value == true then
repeat
fog02.Position = f2pos
MOVE01:Play()
MOVE02:Play()
task.wait(speed)
fog01.Position = f2pos
MOVE03:Play()
MOVE04:Play()
task.wait(speed)
until menu_bool.Value == false
else
MOVE01:Pause()
MOVE02:Pause()
MOVE03:Pause()
MOVE04:Pause()
end
end)
I think what you’re looking for is something like this:
local TweenService = game:GetService("TweenService")
local menu_bool = script.Parent.Parent.Parent.Parent.Stages.Menu
local fogParts = {
script.Parent.fog01,
script.Parent.fog02,
}
-- Configuration
-- Speed at which the fog moves in studs/second. Higher is faster.
local FOG_SPEED_STUDS_PER_SECOND = 5
-- Calculated
-- Where the fog parts first spawn
local FOG_START = fogParts[1].CFrame
local FOG_WIDTH = fogParts[1].Size.X
local TOTAL_FOG_DISTANCE = FOG_WIDTH * #fogParts
-- Where all fog parts eventually end up at the end of the path
local FOG_DESTINATION = FOG_START * CFrame.new(-TOTAL_FOG_DISTANCE, 0, 0)
-- TweenInfo for tweens starting from FOG_START and going to FOG_DESTINATION, repeats indefinitely
local FOG_MOVEMENT_TIME = TOTAL_FOG_DISTANCE / FOG_SPEED_STUDS_PER_SECOND
local tweenInfo = TweenInfo.new(FOG_MOVEMENT_TIME, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1)
-- Tracked to play/pause all tweens at any time
local tweensByPart = {}
for i, fogPart in fogParts do
-- Distribute fog parts along the fog path so that they don't all start at the same spawn
local initialCFrame = FOG_START * CFrame.new(-FOG_WIDTH * (i - 1), 0, 0)
fogPart.CFrame = initialCFrame
-- Calculate the time it takes for the fog part to reach the end of the path.
-- It's different for the initial spawn because each part starts in a different location.
local timeLeft = FOG_MOVEMENT_TIME - (FOG_MOVEMENT_TIME * ((i - 1) / #fogParts))
local initialTweenInfo = TweenInfo.new(timeLeft, Enum.EasingStyle.Linear)
-- Create the initial tween that moves the part from the intermediate spawn to the destination
local initialTween = TweenService:Create(fogPart, initialTweenInfo, { CFrame = FOG_DESTINATION })
-- Once the initial tween is finished, we can reset the CFrame of the fog part to the start and create the "regular" re-usable tween
initialTween.Completed:Connect(function(initialTweenPlaybackState)
if initialTweenPlaybackState ~= Enum.PlaybackState.Completed then
-- If the tween was paused, we do nothing, so that
-- calling :Play() will resume where it left off.
-- Only continue if it actually completed.
return
end
-- Might not be necessary, I just want to make sure our Completed connection gets disconnected so the initialTween can be garbage collected.
initialTween:Destroy()
-- The initial tween completed! Start the "main" re-usable tween that goes all the way from start to destination.
fogPart.CFrame = FOG_START
local reusableTween = TweenService:Create(fogPart, tweenInfo, { CFrame = FOG_DESTINATION })
tweensByPart[fogPart] = reusableTween
reusableTween:Play()
end)
tweensByPart[fogPart] = initialTween
end
local function playFog()
for _, tween in tweensByPart do
tween:Play()
end
end
local function pauseFog()
for _, tween in tweensByPart do
tween:Pause()
end
end
local function update()
if menu_bool.Value then
playFog()
else
pauseFog()
end
end
menu_bool.Changed:Connect(update)
update()