Unable to cast value to Object

Hello, I am currently trying to make a camera parallax and smooth shake system in my main menu. Currently, the parallax works great, but the shake is giving me an error “Unable to cast value to Object” Any ideas?

Here’s my code.

local MenuCamera = {
	Camera = workspace.CurrentCamera,
	cameraCFrame = workspace.MapFolder.Other.MenuScene:WaitForChild("CameraPart").CFrame, -- camera part

	
	MaxXRotationParallax = 15,
	MaxYRotationParallax = 15,
}

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local TweenService = game:GetService("TweenService")
function MenuCamera:Enable()
	if self.Connection then
		self.Connection:Disconnect() -- Disconnect from the Mouse.Move event, if exists
	end

	self.Camera.CameraType = Enum.CameraType.Scriptable

	local function MouseMove()
		local XFloat = (Mouse.X / self.Camera.ViewportSize.X) - 0.5
		local YFloat = (Mouse.Y / self.Camera.ViewportSize.Y) - 0.5
		local _CFrame = self.cameraCFrame
			* CFrame.fromEulerAnglesYXZ(
				math.rad(self.MaxYRotationParallax * -YFloat),
				math.rad(self.MaxXRotationParallax * -XFloat),
				0
			)
		local tweenCam = TweenService:Create(
			self.Camera,
			TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
			{ CFrame = _CFrame }
		)
		tweenCam:Play()
		task.wait(1)
	end

	local function cameraShake()
		local XShake = math.round(math.random(0, self.MaxXRotationParallax))
		local YShake = math.round(math.random(0, self.MaxYRotationParallax))

		local _CFrame = self.cameraCFrame
			* CFrame.fromEulerAnglesYXZ(
				math.rad(self.MaxYRotationParallax * XShake),
				math.rad(self.MaxXRotationParallax * YShake),
				0
			)
		local tweenCam = TweenService:Create(
			self.cameraCFrame,
			TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut),
			{ Orientation = _CFrame }
		)
		tweenCam:Play()
	end
	self.Connection = Mouse.Move:Connect(MouseMove)
	MouseMove()
	while true do
		cameraShake()
	end
end

function MenuCamera:Disable()
	if self.Connection then
		self.Connection:Disconnect() -- Disconnect from the Mouse.Move event, if exists
	end

	self.Camera.CameraType = Enum.CameraType.Custom -- Set the CameraType back to default
end

return MenuCamera

This is a module script.

You can’t tween an object’s property like this. This is because when you do Object.Property you are receiving the value of the property, not a reference to the property.

		local tweenCam = TweenService:Create(
			self.cameraCFrame,
			TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut),
			{ Orientation = _CFrame }
		)

This does not work for the same reason you cant reassign an object’s properties like:

Object.Name = "World"
local Property = Object.Name
Name = "Hello"
print(Object.Name)
-- still "World" because we reassigned the variable "Name" not a reference to Object.Name

You will want to update the camera directly… something like this perhaps?

		local tweenCam = TweenService:Create(
			self.Camera,
			TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut),
			{ CFrame = _CFrame }
		)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.