Does somebody know the answer how to stop a Camera Tween?

The Script is working, when i fire the Event with true and then with false it will break the while after the Tween is finished (50 Sec).

But when i fire the Event with true and then with false and fire it again true before the Tween is finished the latest loop will run forever.

The Break will just work for one while Loop.

And I wrote in the script “tweenCreate:Cancel()” this is breaking the Tween but the cutsceneTime will run anyway.

I tried with cutsceneTime = 0, it didnt worked, does anybody know how could i do it if u know what i mean.

local TweenService = game:GetService("TweenService")

local camera = game.Workspace.Camera

local introCameras = game.Workspace:WaitForChild("IntroCameras")

local breakCamera = false

function tween(part1,part2,cutsceneTime)

	local tweenInfo = TweenInfo.new (
		cutsceneTime,
		Enum.EasingStyle.Quad,
		Enum.EasingDirection.InOut,
		0,
		false,
		0
	)
	
	camera.CFrame = part1.CFrame

	local tweenCreate = TweenService:Create(camera, tweenInfo, {CFrame = part2.CFrame})
	
	tweenCreate:Play()
	
	game.ReplicatedStorage:WaitForChild("CameraMenuAnimation").Event:Connect(function(trueOrFalse)
		
		if trueOrFalse == false then
			
			breakCamera = true
			
			tweenCreate:Cancel()
			workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
			
		end
		
	end)
	
	task.wait(cutsceneTime)

end

game.ReplicatedStorage:WaitForChild("CameraMenuAnimation").Event:Connect(function(trueOrFalse)
		
	
	if trueOrFalse == true then
		
		if camera.CameraType ~= Enum.CameraType.Scriptable then
			
			camera.CameraType = Enum.CameraType.Scriptable
			
		end

		while task.wait() do
			
			if breakCamera == false then

			tween(introCameras.IntroCamera2, introCameras.IntroCamera3, 50)

			if breakCamera == true then

				breakCamera = false
				
				break
				
			end

				tween(introCameras.IntroCamera3, introCameras.IntroCamera2, 50)

				if breakCamera == true then

					breakCamera = false

					break
					
					end

				end

		end
		

	end
		
end)

Tween:Cancel() probably is what you are looking for.

You can use Tween:Cancel().
I’d also use workspace.CurrentCamera instead of game.Workspace.Camera.

Tweening in a continuous loop will not yield, meaning the running thread will not pause and wait for the tween to finish. If the mentioned behavior isn’t intentional, consider creating a debounce to prevent the tween from playing again whilst already playing, and stop tweens using the mentioned Tween:Cancel().

Fun fact, Tween:Wait() either personally gives me trouble or isn’t functional some of the time, so I use my custom function.

local YieldForTween = function(Instance, Properties, ...)
local Tween = TweenService:Create(Instance, TweenInfo.new(... or 0.1), Properties); Tween:Play();
repeat task.wait() until Tween.Completed:Wait();
end;

print(1);
YieldForTween(Part, {Size = Vector3.Zero}, 1); -- Waits for tween to finish more accurately.
print(2);
local Workspace = game:GetService('Workspace');

local Camera = Workspace.CurrentCamera;

Is what I’d personally do, but yeah use Tween:Cancel() , same thing I’ve already stated.

It’s always best to stick to the practices provided by the API.
Exploiters may consider tampering with game.WaitForChild using hookmetamethod to fool the script.

ahh mb mb I mean’t GetService(). NOT WaitForChild()

There is Tween:Cancel but the cutszeneTime kept running

then utilize a simple debounce boolean system.

But i want that the Tween Stop directly, after i saying Tween:Cancel() the cutszeneTime kept running for 50 sec and then the loop will break and when the CameraMenuAnimation Event fires again in the 50 sec the second Loop will never Stop

Ah that would only work with loops, mb if I’m not making sense, it’s currently 3:52 AM lol

But i dont want that the Player has to wait 50 sec for the Tween to start again

what do you mean?, also could you provide video evidence?

Read the “–” Markers then i hope if i explained it good u know what i mean, its long

local TweenService = game:GetService("TweenService")

local camera = game.Workspace.Camera

local introCameras = game.Workspace:WaitForChild("IntroCameras")

local breakCamera = false

function tween(part1,part2,cutsceneTime)

	local tweenInfo = TweenInfo.new (
		cutsceneTime,
		Enum.EasingStyle.Quad,
		Enum.EasingDirection.InOut,
		0,
		false,
		0
	)
	
	camera.CFrame = part1.CFrame

	local tweenCreate = TweenService:Create(camera, tweenInfo, {CFrame = part2.CFrame})
	
	tweenCreate:Play()
	
	game.ReplicatedStorage:WaitForChild("CameraMenuAnimation").Event:Connect(function(trueOrFalse) -- Stopping Camera Tween
		
		if trueOrFalse == false then
			
			breakCamera = true -- This will break the Loop
			
			tweenCreate:Cancel() --Cancel the Tween (cutsceneTime will not stop after that and will run anyway)
			workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
			
		end
		
	end)
	
	task.wait(cutsceneTime)

end

game.ReplicatedStorage:WaitForChild("CameraMenuAnimation").Event:Connect(function(trueOrFalse) -- Playing Camera Tween
		
	
	if trueOrFalse == true then -- trueOrFalse is "true", the script will run, would it be "false" the Event over me inside the tween function would start, what would stop the tween.
		
		if camera.CameraType ~= Enum.CameraType.Scriptable then
			
			camera.CameraType = Enum.CameraType.Scriptable
			
		end

		while task.wait() do
				
			tween(introCameras.IntroCamera2, introCameras.IntroCamera3, 10) -- Tween need 10 sec to tween from Part 1 to Part 2
			
			if breakCamera == true then -- If trueOfFalse is "false" the Event over me inside the tween function will run, what will make breakCamera to true. If its true the Loop will break. But there is a Problem. If a Tween is running and another Event with trueOrFalse == true is firing this loop will start again. Now are 2 Loops running. After the first Loop is breaked because of the "If breakCamera == true", the second one will not break because breakCamera is false. It would work if i put local breakCamera into this Event and not outside the Event, but then i cant change the breakCamera outside the script. 
				
				breakCamera = false
				
				break
				
			end
			
			tween(introCameras.IntroCamera3, introCameras.IntroCamera2, 10)
			
			if breakCamera == true then
				
					breakCamera = false

					break
					
					end

				end

	end
		
end)
  1. Are you making sure the condition is met? (is trueOrFalse = false when the event is fired?)

  2. If parameters in your tween are 0 or false, there is no need to state them, they are those values by default I believe.

trueOrFalse will be false inside the Tween Function and under the Tween Function there is also a Event what will be fired when trueOrFalse is true

I thing one Problem here is that local breakCamera will be false and that means that the other Loops will not break the Loop but how can i make seperate variables then? Idk

--/// Services:
local ReplicatedStorage = game:GetService('ReplicatedStorage');
local TweenService = game:GetService('TweenService');
local Workspace = game:GetService('Workspace');

--/// Constants:
local Camera = Workspace.CurrentCamera;

local CameraMenuEvent = ReplicatedStorage:WaitForChild('CameraMenuAnimation')
local IntroCameras = Workspace:WaitForChild('IntroCameras');

--/// Variables:
local BreakCamera = false;

--/// Local Functions:
local Tween = function(CFrame1: Cframe, CFrame2: CFrame, Time: Number)
local TweenInformation = TweenInfo.new(Time, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut);
local TweenCreate = TweenService:Create(Camera, TweenInformation, {CFrame = CFrame2});

Camera.CFrame = CFrame1;
TweenCreate:Play();

local Function = function(Boolean: Boolean) print(Boolean);
if Boolean then return end;

BreakCamera = true;
TweenCreate:Cancel();
Camera.CameraType = Enum.CameraType.Custom;
end;

return CameraMenuEvent.Event:Connect(Function);
end;

local Function1 = function(Boolean: Boolean) print(Boolean);
if not Boolean then return end;

local IntroCamera2, IntroCamera3 = IntroCameras:WaitForChild('IntroCamera2'), IntroCameras:WaitForChild('IntroCamera3');

if Camera.CameraType ~= Enum.CameraType.Scriptable then
Camera.CameraType = Enum.CameraType.Scriptable;
end;

while task.wait() do
Tween(IntroCamera2.CFrame, IntroCamera3.CFrame, 10);

if BreakCamera then
BreakCamera = false;
break;
end;

Tween(IntroCamera3.CFrame, IntroCamera2.CFrame, 10);

if BreakCamera then
BreakCamera = false;
break;
end;
end;
end;

--/// Connections:
CameraMenuEvent.Event:Connect(Function1);
1 Like

Better formatted, optimized, etc.

No need to constantly get said child, just store them in a variable!

either run this code, or add print(trueOrFalse) when CameraMenuAnimation gets fired and tell me what it says.

Thanks you! I will try it out now