My Problem With The Camera Tween

Hi, Maskedivity here. You can probably assume I am new to the forums, and you are right. All I ask as my first post is help with this small issue I am having.

  1. The only thing I want to achieve is my problem being fixed, and hopefully getting some advice.

  2. My problem is when I click a gui button, it plays a tween from two parts that act as cameras, but when the tween ends, the tween resets to the start camera, instead of the camera keeping its position in the destination camera.

  3. I looked for a help that I could try to use, but none of them worked.

I would really appreciate any help.

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Camera = workspace.CurrentCamera
local Mouse = game.Players.LocalPlayer:GetMouse()
local DefaultCFrame = game.Workspace.MainMenu.MainDetails.CameraPart.CFrame
local Scale = 700
local TweenService = game:GetService("TweenService")
local button = game.Players.LocalPlayer.PlayerGui.MenuGui.MainFrame.PlayButton


button.MouseButton1Click:Connect(function()
	local tweenService = game:GetService("TweenService")

	workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable

	workspace.CurrentCamera.CFrame = game.Workspace.MainMenu.MainDetails.CameraPart.CFrame

	local tweenInfo = TweenInfo.new(2.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	
	local dest = {}
	dest.CFrame = game.Workspace.MainMenu.MainDetails.CameraPart2.CFrame

	local tween = tweenService:Create(workspace.CurrentCamera, tweenInfo, dest)

	tween:Play()
end)

local dest = {}
dest.CFrame = game.Workspace.MainMenu.MainDetails.CameraPart2.CFrame

repeat wait() 
	Camera.CameraType = Enum.CameraType.Scriptable 
until Camera.CameraType == Enum.CameraType.Scriptable 

game:GetService("RunService").RenderStepped:Connect(function() 
	Camera.Focus = DefaultCFrame
	local Center = Vector2.new(Camera.ViewportSize.X/25, Camera.ViewportSize.Y/25)
	local MoveVector = Vector3.new((Mouse.X-Center.X)/Scale, -(Mouse.Y-Center.Y)/Scale, 0)
	Camera.CFrame = DefaultCFrame * CFrame.Angles(math.rad(MoveVector.X), math.rad(MoveVector.Y), math.rad(MoveVector.Z))
	Camera.FieldOfView = 65
end)

(yes i used LittleJackBlocks script)

1 Like

This is the camera animation method I personally use, forgot where I got it from, but it works really well and should be able to solve the issue you are facing. Btw this would be a LocalScript in StarterGui

local camera = workspace.CurrentCamera
local TweenService = game:GetService("TweenService")

local function MoveCamera(StartPart, EndPart, Duration, EasingStyle, EasingDirection)
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = StartPart.CFrame
local Cutscene = TweenService:Create(camera, TweenInfo.new(Duration, EasingStyle, EasingDirection), {CFrame = EndPart.CFrame})
Cutscene:Play()
end

MoveCamera(game.Workspace.StartCameraPart, game.Workspace.EndCameraPart, 5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, true) --Example (you would probably want to change the camera parts and mess around with these parameters)

--To give the player the control of their camera again just run: camera.CameraType = Enum.CameraType.Custom

Sorry but the script doesn’t work. I’m very sorry, and the problem is that when I click the play button, when the tween plays, instead of the camera staying at the destination camera, it resets and goes back to the start camera.

The camera CFrame is being set to this

Camera.CFrame = DefaultCFrame * CFrame.Angles(math.rad(MoveVector.X), math.rad(MoveVector.Y), math.rad(MoveVector.Z))

every frame, so once the tween is over, it will go back to this CFrame.

You could do,

local TweenService = game:GetService("TweenService")
local EndPoint = workspace.whateveritscalled -- ending block

local CurrentCamera = workspace.CurrentCamera
CurrentCamera.CameraType = Enum.CameraType.Scriptable

local tween = TweenService:Create(CurrentCamera, TweenInfo.new(2.5), {CFrame = EndPoint.CFrame})

This is just a rough example, you can make your changes and put it in the function, but this also makes it a bit more organized.

So should I get rid of that line?

Edit: I’m also getting an identifier with parsing error on one of your lines.

local function MoveCamera(StartPart, EndPart, 2.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, true)

Edit again: I appreciate both of you helping me, but I don’t think any of the issues are because of the tween, I think its because of the script that the screen move when you move your mouse.

    repeat wait() 
Camera.CameraType = Enum.CameraType.Scriptable 

until Camera.CameraType == Enum.CameraType.Scriptable

game:GetService("RunService").RenderStepped:Connect(function() 
Camera.Focus = DefaultCFrame
local Center = Vector2.new(Camera.ViewportSize.X/25, Camera.ViewportSize.Y/25)
local MoveVector = Vector3.new((Mouse.X-Center.X)/Scale, -(Mouse.Y-Center.Y)/Scale, 0)
Camera.CFrame = DefaultCFrame
Camera.FieldOfView = 65

end)

Anything wrong?

Edit one more time: Sorry about the mess with the code, I’m still new to formatting these texts.

But then how am I to fix this problem, remove the line?
Edit it?

Reply was supposed to send to Sterpant’s reply to LiegePalpatine’s reply.

I’ve tried all of your options, but none of them work. I don’t think my issue is that your scripts don’t work, like I and Sterpant said before, it has something to do with this: My Problem With The Camera Tween - #6 by Gaabory

You would change DefaultCameraCFrame to the destination part once the tween is completed. The reason why the camera is going back to the start position is because DefaultCameraCFrame has been set to the starting position:

-- line 5
local DefaultCFrame = game.Workspace.MainMenu.MainDetails.CameraPart.CFrame

and every frame the camera CFrame is being set to DefaultCameraCFrame:

-- line 41
Camera.CFrame = DefaultCFrame * CFrame.Angles(math.rad(MoveVector.X), math.rad(MoveVector.Y), math.rad(MoveVector.Z))

If you wait for the tween to complete, and then set DefaultCFrame to the destination CFrame, that will fix your problem.

Inside of the MouseButton1Click event—see my comment:

button.MouseButton1Click:Connect(function()
	local tweenService = game:GetService("TweenService")

	workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable

	workspace.CurrentCamera.CFrame = game.Workspace.MainMenu.MainDetails.CameraPart.CFrame

	local tweenInfo = TweenInfo.new(2.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)

	local dest = {}
	dest.CFrame = game.Workspace.MainMenu.MainDetails.CameraPart2.CFrame

	local tween = tweenService:Create(workspace.CurrentCamera, tweenInfo, dest)

	tween:Play()
	
	--[[
		Wait for tween to complete, and then set `DefaultCFrame`
		to the destination CFrame 
	]]
	tween.Completed:Wait()
	DefaultCFrame = game.Workspace.MainMenu.MainDetails.CameraPart2.CFrame
end)

And if you wanted to set up a debounce, this is how you would do it:

local tweening = false

button.MouseButton1Click:Connect(function()
	if not tweening then
		tweening = true

        -- code

		tweening = false
	else
		return		
	end
end)

Hope this helps.

Sterpant, I can’t thank you enough. I have searched high and low for an answer and the only reason I never decided to look/ask here was because I was scared.

If there is anything I can do to thank you please do let me know.

1 Like

Sorry to bother you but I may have messed something up with the script. I wanted to move the camera mouse move to one seperate script, and instead of saving the original script, I completely changed it, and now the tween resets to the start again. I’m really sorry for the trouble but could you please help me fix my problem.

On the line below tween.Completed:Wait(), remove the .CFrame so it’s just:

DefaultCFrame = game.Workspace.MainMenu.MainDetails.CameraPart2

:slight_smile:

This has a lot more lines of code in it than it needs

It works now, but when I load into the game, instead of staying on the start cam, it goes to my player cam, then when I press play, it starts the tween.

Edit: This error also comes up:
image

Nevermind I fixed it. Thank you for both of your helps though.