Camera Tweening Not Working

I’m creating a system where you can sleep in and exit a tent. Whenever you enter the tent, it plays a little animation for the camera (which works). When you exit the tent, it should play an animation that tweens the camera back to where it should be.

Why is this not working??

–script 1

return function(data, activatedByPlayer, variables, id)
	
	local CameraController = Knit.GetController("CameraController")
	local CurrentCamera = workspace.CurrentCamera

	local char = LocalPlayer.Character
	if not char then return end
	
	local humRoot = char:WaitForChild("HumanoidRootPart")
	
	
	local offset = humRoot.CFrame:ToObjectSpace(CurrentCamera.CFrame)
	local target = humRoot.CFrame:ToWorldSpace(offset)

	for i, model in Placeables:GetChildren() do

		if model.id.Value == id and not variables.PlayerUsing then
			
			if activatedByPlayer then
				CameraController:RemoveCinematic()
				CameraController:SwitchCameraMode("Default", true, target, TweenInfo.new(0.3, Enum.EasingStyle.Sine))
			end

			model.Flaps.Transparency = 1
		elseif model.id.Value == id and variables.PlayerUsing then
			model.Flaps.Transparency = 0

			if activatedByPlayer then
				CameraController:SwitchCameraMode("Locked")
				CameraController:AddCinematic()
			end
			
		end

	end

end

– script 2
(where the tweening should happen)

function CameraController:SwitchCameraMode(mode, smooth, target, info)
	if mode == "Locked" then

		self.ModeChanged:Fire()

		self.CurrentMode = "Locked"
		self.CharacterController:LockCharacter()
		self.Camera.CameraType = Enum.CameraType.Scriptable

	elseif mode == "Default" then

		self.ModeChanged:Fire()
		
		local tween
		
		if smooth and target and info then
			tween = TweenService:Create(self.Camera, info, {CFrame = target})
			tween:Play()
		end

		if tween then
			self.Trove:Connect(tween.Completed, function ()
				self.CharacterController:UnlockCharacter()
				self.CurrentMode = "Default"
				self.Camera.CameraType = Enum.CameraType.Custom
			end)
		end
		
		if tween then return end
		self.CharacterController:UnlockCharacter()
		self.CurrentMode = "Default"
		self.Camera.CameraType = Enum.CameraType.Custom
	end
end

–also this is only a snippet of the script; not the full this

if anyone has any ideas, let me know.

Thanks!

Tween returns a https://create.roblox.com/docs/reference/engine/classes/Tween which isn’t true. Since statements run only when their conditions are true your code won’t even run in the first place. Check if it isn’t nil instead Tween ~= nil.
Also it would be easier and less messier to pcall the tween making since it will just error and half execution entirely instead of fallbacking like you intended.

im pretty sure the statement “if tween then” would evaluate to true as long as the tween exists.

local tween = game.TweenService:Create(Instance.new("Part"), TweenInfo.new(1), {})
if tween then
	print(1)
end

after running this code in a script it did print 1 into the output

1 Like

also it would be much easier to figure out the issue if i was able to test this out for myself by recreating the problem but it seems like your using things like knit and trove which would take a while to set up. are you able to provide a place file so we can recreate the problem exactly?

1 Like

“if expressions conditionally evaluate expressions and return the value produced as a result”
6 > 7 is false and so the script provided in the article above will return 7 because of the else.

so that is actually an if expression not an if statement. they don’t work the same way.
heres the rest of the context behind the quote you provided that proves this

“In addition to supporting standard if statements , Luau adds support for if expressions . Syntactically, if-then-else expressions look very similar to if statements. However instead of conditionally executing blocks of code, if expressions conditionally evaluate expressions and return the value produced as a result.”

In Luau any value that isn’t nil or false is automatically true

function CameraController:SwitchCameraMode(mode, smooth, target, info)
	if mode == "Locked" then

		self.ModeChanged:Fire()

		self.CurrentMode = "Locked"
		self.CharacterController:LockCharacter()
		self.Camera.CameraType = Enum.CameraType.Scriptable

	elseif mode == "Default" then

		self.ModeChanged:Fire()
		
		local tween
		
		if smooth and target and info then
			tween = TweenService:Create(self.Camera, info, {CFrame = target})
			tween:Play()
		end

		if tween then
			self.Trove:Connect(tween.Completed, function ()
				self.CharacterController:UnlockCharacter()
				self.CurrentMode = "Default"
				self.Camera.CameraType = Enum.CameraType.Custom
			end)
		end
		
		if tween then return end
		self.CharacterController:UnlockCharacter()
		self.CurrentMode = "Default"
		self.Camera.CameraType = Enum.CameraType.Custom
	end
end

I use if tween because the player can choose if they want to tween or not, if smooth == true then a tween is created, if smooth == false then a tween isn’t created so nothing will happen.

theres probably a better way to do that but thats what im working with right now.

ALSO

thats not really the problem

My problem is the tween doesn’t play (like at all) but everything prints like it does play.

Should i just use :Lerp to tween it to their head? and if so could you provide a script as a reference or something.

My goal is after the script plays the cutscene (while the camera mode is scriptable) I want it to tween to where the player’s camera would be and then revert the camera mode to default/custom.

Thanks for the input so far though!