Tween not being cancelled

I have the camera tween around the map when a player joins (like cinematic type stuff)

function CameraManipulation:Intro()
	Camera.CameraType = Enum.CameraType.Scriptable
	
	local InfoTween = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
	
	Camera.CFrame = CFrame.new(CameraPoints['1-1'].CFrame.Position)
			
	local Tween = TweenService:Create(Camera, InfoTween, {CFrame = CameraPoints['1-2'].CFrame})
	Tween:Play()
	
	if Customising or StopIntro then Tween:Cancel() return end
	
	wait(6)
	
	if Customising or StopIntro then Tween:Cancel() return end
	
	Camera.CFrame = CFrame.new(CameraPoints['2-1'].CFrame.Position)
			
	local Tween = TweenService:Create(Camera, InfoTween, {CFrame = CameraPoints['2-2'].CFrame})
	Tween:Play()
	
	if Customising or StopIntro then Tween:Cancel() return end
	
	wait(6)
	
	if Customising or StopIntro then Tween:Cancel() return end
	
	Camera.CFrame = CFrame.new(CameraPoints['3-1'].CFrame.Position)
			
	local Tween = TweenService:Create(Camera, InfoTween, {CFrame = CameraPoints['3-2'].CFrame})
	Tween:Play()
	
	if Customising or StopIntro then Tween:Cancel() return end
	
	wait(6)
	
	if Customising or StopIntro then Tween:Cancel() return end
	
	Camera.CFrame = CFrame.new(CameraPoints['4-1'].CFrame.Position)
			
	local Tween = TweenService:Create(Camera, InfoTween, {CFrame = CameraPoints['4-2'].CFrame})
	Tween:Play()
end

Now if a player is new, it’ll bring up the customise menu, which sets the camera at a different positon.

function CameraManipulation:StartCustomise()
	Customising = true
	
	wait()
	
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.FieldOfView = 60
	Camera.CameraSubject = CustomiseRoom.Camera
	Camera.CFrame = CustomiseRoom.Camera.CFrame * CFrame.Angles(math.rad(350), 0, 0)
end

Problem is, the camera still stays in the intro tweening. I tried

if Customising or StopIntro then Tween:Cancel() return end

It finishes off the current tween, then just stops.


and stays in the one spot forever

Camera.CameraType = Enum.CameraType.Scriptable
Camera.FieldOfView = 60
Camera.CameraSubject = CustomiseRoom.Camera
Camera.CFrame = CustomiseRoom.Camera.CFrame * CFrame.Angles(math.rad(350), 0, 0)

The wait() if then cancel end seems a bit wacky to me. Instead, why not save the tween in a global variable (or member variable if you are using oop)

When calling :StartCustomization(), you can just cancel/destroy etc the tween via the variable

1 Like

Not entirely sure what you mean here?

so declare
local Tween;
above your functions or as a member variable of class CameraManipulsruon

In :Intro(), assign the tween that you create to the variable above

This variable is accessible from inside the :StartCustomize() function, which you can cancel/destroy/pause etc without any wait or conditional statements

local Tween

function CameraManipulation:Intro()
	Camera.CameraType = Enum.CameraType.Scriptable
	
	local InfoTween = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
	
	Camera.CFrame = CFrame.new(CameraPoints['1-1'].CFrame.Position)
			
	Tween = TweenService:Create(Camera, InfoTween, {CFrame = CameraPoints['1-2'].CFrame})
	Tween:Play()

	wait(6)

	Camera.CFrame = CFrame.new(CameraPoints['2-1'].CFrame.Position)
			
	Tween = TweenService:Create(Camera, InfoTween, {CFrame = CameraPoints['2-2'].CFrame})
	Tween:Play()

	wait(6)
	
	Camera.CFrame = CFrame.new(CameraPoints['3-1'].CFrame.Position)
			
	Tween = TweenService:Create(Camera, InfoTween, {CFrame = CameraPoints['3-2'].CFrame})
	Tween:Play()
	
	wait(6)
	
	Camera.CFrame = CFrame.new(CameraPoints['4-1'].CFrame.Position)
			
	Tween = TweenService:Create(Camera, InfoTween, {CFrame = CameraPoints['4-2'].CFrame})
	Tween:Play()
end

function CameraManipulation:StopIntro()
	if Tween then
		Tween:Cancel()
	end
end

Still doesn’t work. The camera goes to the player, but then goes back to tweening and finishing off the Intro function of tweens

The problem is that you are just canceling the current tween and not exiting from the Intro() function. Add a control boolean at the top and then check if the intro has been canceled after each wait.

local IntroCanceled = false
local Tween = nil

function CameraManipulation:Intro()
    ...
    Tween = TweenService:Create(...)
    Tween:Play()
    
    wait(6)
    if IntroCanceled then
        return
    end
    ...
end

function CameraManipulation:StopIntro()
    if Tween then
    	Tween:Cancel()
    end
    IntroCanceled = true
end