Pressing right click on the mouse will overwrite the camera type and set it back to the default camera type. This is only happening inside of studio and doesnt happen inside of the published game. Im not sure if its a bug or what, but its really annoying.
Anyways heres the code:
local CameraModule = {}
CameraModule.__index = CameraModule
local replicatedStorage = game:GetService("ReplicatedStorage")
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local players = game:GetService("Players")
local replicatedModules = replicatedStorage:WaitForChild("Modules")
local enums_Modules = replicatedModules.Enums
local sharedEnums = require(enums_Modules.SharedEnums)
function CameraModule.new(Character: Model)
local self = setmetatable({}, CameraModule)
self.Character = Character
self.Player = players.LocalPlayer
self.Mouse = self.Player:GetMouse()
self.Camera = workspace.CurrentCamera
self.playerModule = self.Player:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")
self.CurrentMode = nil
self.CameraStationCFrame = CFrame.new(0, 8, 0)
self.Connections = {}
self._UpdateMenuCamera = function(DeltaTime: number)
local viewportSize = self.Camera.ViewportSize
local XOffset = math.rad(((self.Mouse.X - viewportSize.X / 2) / viewportSize.X) * -15)
local YOffset = math.rad(((self.Mouse.Y - viewportSize.Y / 2) / viewportSize.Y) * -15)
self.Camera.CFrame = self.Camera.CFrame:Lerp(
self.CameraStationCFrame * CFrame.Angles(YOffset, XOffset, XOffset / 2),
DeltaTime * 2
)
end
return self
end
function CameraModule:SetGameCameraMode(CameraMode: string)
if self.CurrentMode == CameraMode then return end
self.CurrentMode = CameraMode
if CameraMode == sharedEnums.GameCameraModes.Menu then
self:_SetMode_Menu()
elseif CameraMode == sharedEnums.GameCameraModes.Game then
self:_SetMode_Game()
end
end
function CameraModule:Destroy()
self.CurrentMode = nil
disconectEvents(self.Connections)
end
function CameraModule:_SetMode_Menu()
self.Camera.CameraType = Enum.CameraType.Scriptable
self.Camera.CFrame = self.CameraStationCFrame
runService:BindToRenderStep(
"MenuCamera",
Enum.RenderPriority.Camera.Value,
self._UpdateMenuCamera
)
end
function CameraModule:_SetMode_Game()
self.Camera.CameraType = Enum.CameraType.Custom
runService:UnbindFromRenderStep("MenuCamera")
end
function disconectEvents(Events: {})
if not Events then return end
for _, connection in Events do
if typeof(connection) == "RBXScriptConnection" then
connection:Disconnect()
end
end
end
return CameraModule