Tween is in loop and can't stop it

I want to have a main menu which has 4 parts that go though the map to show a ariel view of the map. I got his part all figured out and working but I also want to have that tween stop and switch camera view to another angle (not tweening it there). I have spent many hours trying to fix this, looking on the dev forum and testing many solutions. Closest thing I have gotten to was I paused all tweens and switched the camera but after a few seconds it started the other tween again. Any help would be great! Sorry if the code is not easy to read!

local Tween = game:GetService("TweenService")

local Camera = game.Workspace.CurrentCamera
local work = game.Workspace.Menu
local Player = game.Players.LocalPlayer
local playbutton = script.Parent.Play
local transition = script.Parent.Transition

wait(.001)

Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = work.Cam1.CFrame


local targetTransparency1 = 0
local targetTransparency2 = 1
local Played = true

local tweenInfo = TweenInfo.new(0.15)
local fadein = Tween:Create(transition, tweenInfo, {BackgroundTransparency = targetTransparency1})
local fadeout = Tween:Create(transition, tweenInfo, {BackgroundTransparency = targetTransparency2})


local tween1 = Tween:Create(Camera,TweenInfo.new(20, Enum.EasingStyle.Linear), {CFrame = work.Cam2.CFrame})
local tween2 = Tween:Create(Camera,TweenInfo.new(0.001, Enum.EasingStyle.Linear), {CFrame = work.Cam3.CFrame})
local tween3 = Tween:Create(Camera,TweenInfo.new(20, Enum.EasingStyle.Linear), {CFrame = work.Cam4.CFrame})
local tween4 = Tween:Create(Camera,TweenInfo.new(0.001, Enum.EasingStyle.Linear), {CFrame = work.Cam1.CFrame})


 function TCP()

	if Played == true then
		print("playing again")
		tween1:Play()
		task.wait(20)
		tween2:Play()
		--Camera.CFrame = work.Cam3.CFrame
		task.wait(0.001)
		tween3:Play()	
		task.wait(20)
		tween4:Play()
		--Camera.CFrame = work.Cam1.CFrame
		task.wait(0.001)
		TCP()
	
	else
	print("Not playing tween again")
	return
		end

	
end



playbutton.MouseButton1Click:Connect(
	function ()
		Played = false
		print("Menu switched cameras")
		fadein:Play()
		task.wait(0.15)
		local thread = task.spawn(TCP)
		task.cancel(thread)
		tween1:Cancel()
		tween2:Cancel()
		tween3:Cancel()
		tween4:Cancel()
		work.Cam1:Destroy()
		work.Cam2:Destroy()
		work.Cam3:Destroy()
		work.Cam4:Destroy()
		--Camera.CameraType = Enum.CameraType.Custom
		--Camera.CameraSubject = Player.Character
		Camera.CameraType = Enum.CameraType.Scriptable
		Camera.CFrame = work.Cam5.CFrame
		wait(0.4)
		fadeout:Play()
	end
)





TCP()

Do you maybe have a video, so I can understand it better?

Yes here is the video (Also sorry for such a low FPS but it shows what it is doing)

The function must’ve been running WHILE you set the Playing value to false, meaning the function still thinks Played is true because it hasn’t checked again. Instead, make sure Played is still true in each of those 20 seconds.

if Played == true then
	print("playing again")
	tween1:Play()
	task.wait(20)
	if Played == false then --Check if Played is still true
		return --if not, stop the function
	end
	tween2:Play()
	--Camera.CFrame = work.Cam3.CFrame
	task.wait(0.001)
	if Played == false then--Check if Played is still true
		return
	end
	tween3:Play()	
	task.wait(20)
	if Played == false then--Check if Played is still true
		return
	end
	tween4:Play()
	--Camera.CFrame = work.Cam1.CFrame
	task.wait(0.001)
	if Played == false then--Check if Played is still true
		return
	end
	TCP()

else
-- code continues
1 Like

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