Tween Cam does not work

Well, I’ve been wanting to make a TweenCam that when pressing a text button changes the position of the camera to a part with the TweenService, but when clicking the text button again to return it to the humanoid but doesn’t working

local Workspace = game:GetService("Workspace")
local TweenService = game:GetService("TweenService")

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character

local CurrentCamera = Workspace.CurrentCamera

local isOpen = false

script.Parent.MouseButton1Down:Connect(function()
	isOpen = not isOpen
	if isOpen then
		CurrentCamera.CameraType = Enum.CameraType.Scriptable
		local tweenInfo = TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0)
		local CameraStartPart = {CFrame = Workspace:WaitForChild("CameraStart").CFrame}
		local CameraTweenStart = TweenService:Create(CurrentCamera, tweenInfo, CameraStartPart)
		CameraTweenStart:Play()
		script.Parent.Text = "Abierto"
	else
		CurrentCamera.CameraType = Enum.CameraType.Scriptable
		local tweenInfo = TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0)
		local CameraStartPart = {CFrame = Character:WaitForChild("Humanoid")}
		local CameraTweenStart = TweenService:Create(CurrentCamera, tweenInfo, CameraStartPart)
		CameraTweenStart:Play()
		CameraTweenStart.Completed:Wait()
		CurrentCamera.CameraType = Enum.CameraType.Custom
		script.Parent.Text = "Cerrado"
	end
end)

Replace this with Character:WaitForChild("HumanoidRootPart").CFrame

1 Like

Hello @NUTRICORP ,

I believe your problem is that in your second case, you don’t specify an actual CFrame:

Your target CFrame is a Humanoid Instance which will fail.
Apart from this, if both of your tweens are identical always, I’d recommend you declare the tweens outside the mouse connect listener. It is much more efficient, otherwise you are creating Tween Instances on every click :slight_smile: .

Try changing to this and let me know if it works:

local tweenInfo = TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0)
local CameraStartPart = {CFrame = Workspace:WaitForChild("CameraStart").CFrame}
local CameraTweenStart = TweenService:Create(CurrentCamera, tweenInfo, CameraStartPart )
local CameraStartPart2 = {CFrame = Character:WaitForChild("HumanoidRootPart").CFrame}
local CameraTweenStart2 = TweenService:Create(CurrentCamera, tweenInfo, CameraStartPart2)

script.Parent.MouseButton1Down:Connect(function()
	isOpen = not isOpen
	if isOpen then
		CurrentCamera.CameraType = Enum.CameraType.Scriptable
		CameraTweenStart:Play()
        CameraTweenStart.Completed:Wait()
		script.Parent.Text = "Abierto"
	else
		CurrentCamera.CameraType = Enum.CameraType.Scriptable
		CameraTweenStart2:Play()
		CameraTweenStart2.Completed:Wait()
		CurrentCamera.CameraType = Enum.CameraType.Custom
		script.Parent.Text = "Cerrado"
	end
end)

Yes it works but not the way I expected as I want it to snap back to its original position and not jump right when the Tween of CameraTweenStart2 ends

If you want it to snap, then don’t use a tween :slight_smile: !
Here, try this:

local tweenInfo = TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0)
local CameraStartPart = {CFrame = Workspace:WaitForChild("CameraStart").CFrame}
local CameraTweenStart = TweenService:Create(CurrentCamera, tweenInfo, CameraStartPart )

script.Parent.MouseButton1Down:Connect(function()
	isOpen = not isOpen
	if isOpen then
		CurrentCamera.CameraType = Enum.CameraType.Scriptable
		CameraTweenStart:Play()
        CameraTweenStart.Completed:Wait()
		script.Parent.Text = "Abierto"
	else
		CurrentCamera.CFrame = Character:WaitForChild("HumanoidRootPart").CFrame
		CurrentCamera.CameraType = Enum.CameraType.Custom
		script.Parent.Text = "Cerrado"
	end
end)

Now the camera teleports and does not do the tween, thanks anyway

Oh okay, I think now I understood, the explanation was just not very clear. I think this is what you are looking for. Please do let me know if it works :smiley: :

local tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0)
local CameraStartPart = {CFrame = Workspace:WaitForChild("CameraStart").CFrame}
local CameraTweenStart = TweenService:Create(CurrentCamera, tweenInfo, CameraStartPart )

local cameraOffset = nil

local isOpen = false
script.Parent.MouseButton1Down:Connect(function()
	isOpen = not isOpen
	if isOpen then
		CurrentCamera.CameraType = Enum.CameraType.Scriptable
		cameraOffset = Character:WaitForChild("HumanoidRootPart").CFrame:inverse() * CurrentCamera.CFrame
		CameraTweenStart:Play()
		CameraTweenStart.Completed:Wait()
		script.Parent.Text = "Abierto"
	else
		CurrentCamera.CameraType = Enum.CameraType.Scriptable
		local CameraStartPart2 = {CFrame = Character:WaitForChild("HumanoidRootPart").CFrame * cameraOffset}
		local CameraTweenStart2 = TweenService:Create(CurrentCamera, tweenInfo, CameraStartPart2)
		CameraTweenStart2:Play()
		CameraTweenStart2.Completed:Wait()
		CurrentCamera.CameraType = Enum.CameraType.Custom
		script.Parent.Text = "Cerrado"
	end
end)
1 Like

I need help with something, what you gave me I am using as a camera intro but it gives me an error invalid argument #2 (Vector3 expected, got nil)
The error is given to me on line 19

local Workspace = game:GetService("Workspace")

local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0)

local CameraTweenTwo = TweenService:Create(CurrentCamera, tweenInfo, {CFrame = Parts.TwoPart.CFrame})
local CameraTweenThree = TweenService:Create(CurrentCamera, tweenInfo, {CFrame = Parts.ThreePart.CFrame})

local cameraOffset = nil

local function CamFinish()
	local Player = game:GetService("Players").LocalPlayer
	local Character = Player.Character
	CameraTweenTwo:Play()
	CameraTweenTwo.Completed:Wait()
	wait(0.15)
	CameraTweenThree:Play()
	CameraTweenThree.Completed:Wait()
	wait(0.15)
	local CameraStartPart2 = {CFrame = Character:WaitForChild("HumanoidRootPart").CFrame * cameraOffset}
	local CameraTweenFour= TweenService:Create(CurrentCamera, tweenInfo, CameraStartPart2)
	CameraStartPart2:Play()
	CameraStartPart2.Completed:Wait()
	CurrentCamera.CameraType = Enum.CameraType.Custom
end

Can you maybe tell me which line 19 is exactly? As it might vary here.

local CameraStartPart2 = {CFrame = Character:WaitForChild("HumanoidRootPart").CFrame * cameraOffset}

Regarding this value of cameraOffset, you never really say what it is, that’s the problem. In my answer I had used this:

Try using the same value for cameraOffset but before the first tweens. It must be set when the camera is still on the player.

1 Like