Overriding the default roblox camera

I am trying to create a title screen that sets the player camera as soon as the game is loaded, however, when I run the script the camera get’s into position but the roblox engine then sets it back to my character since it has just spawned in. My current solution is to delay the script by a small amount of time (1-2 seconds) but I was wandering if there was a way to delete the default camera process.

script for reference:

if not game:IsLoaded() then
game.Loaded:Wait()
end

wait(2)

local Players = game:GetService(“Players”)
local localPlayer = Players.LocalPlayer
local Workspace = game:GetService(“Workspace”)

local currentCamera = Workspace.CurrentCamera

local newCamera = Instance.new(“Camera”)

newCamera.Name = “TitleScreenCamera”
newCamera.Parent = game.Workspace:WaitForChild(“TitleScreen”)
game.Workspace.TitleScreen:WaitForChild(“TitleScreenCamera”)
newCamera.Name = “TitleScreenCamera”
newCamera.CameraType = Enum.CameraType.Scriptable
newCamera.CFrame = Workspace.TitleScreen.CameraPos.CFrame
currentCamera.CameraType = Enum.CameraType.Scriptable

Workspace.CurrentCamera = newCamera

The main issue in your code is the line Workspace.CurrentCamera = newCamera. You’re pretty much saying that both the newCamera and the Workspace.CurrentCamera are the exact same. What I’d suggest that you do is set the Camera’s CFrame as the exact one of the newCamera. Plus, change the category from this post from Platform Usage Support to Scripting Support!

Workspace.CurrentCamera.CFrame = CFrame.new(newCamera.Position) * CFrame.fromOrientation(
   math.rad(newCamera.Orientation.X),
   math.rad(newCamera.Orientation.Y),
   math.rad(newCamera.Orientation.Z)
)

What this code is doing is precisely extracting both the position and all three rotation angles from the newCamera’s instance and setting them as the CurrentCamera’s CFrame. If you have any other questions, just let me know! :wink:

1 Like

thanks for the help, the camera takes the position the the issue still stands, without a delay roblox sets my camera back to my avatar unless there is a delay

Gotcha. Spawn the code inside a separate thread then with a while "boolean" do loop so that Roblox won’t automatically reset the camera’s view.

task.spawn(function()
   while "condition" == true do
      -- Previous code goes here
      if "condition_met" then
         "condition" = false
      end
   end
end

The task.spawn(function()) call is spawning the code in an asynchronous thread so it won’t interrupt with the rest of the code, and the if - then call breaks the loop by changing a set condition to false. You need to replace “condition” and “condition_met” with any events that will impact the camera, such as a button being clicked whatsoever.

While that solution works, it’s also very laggy. I was thinking of one where you physically alter the cameramoldule, so that it only spawns the player camera under certain conditions.

edit: it just crashed my studio

1 Like

You could wrap it inside another function call and spawn it only when necessary, since it’s probably eating up a bunch of bytes to run that.

function setCameraView()
   -- Paste the whole thing in here
end

-- Run the function under a certain condition

Also, RIP your studio. I think my failsafe solution may have wrecked the poor thing… .-.

Do you know of anyway to modify the default camera module so that it only runs under certain conditions?

1 Like

As of right now, I don’t really know if I have an answer. I could investigate in an old project of mine that has a functional camera-shifting script.

Edit - Here’s the code I found in that project. You can feel free to use it for the single camera module you want! By the way, it’s set as a LocalScript running inside a StarterGUI instance.

local _Workspace = game:GetService("Workspace")

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

local previousCamera = nil
local currentCamera = nil

local CAMERA_VIEW_UPDATE_INTERVAL = 10

local CAMERAS = {
	camera_1 = {Position = Vector3.new(157.41, 376.761, 113.038), Orientation = Vector3.new(-15, 60, 0)},
	camera_2 = {Position = Vector3.new(-102.48, 405.523, -235.285), Orientation = Vector3.new(-30, 150, 0)},
	camera_3 = {Position = Vector3.new(256.692, 392.523, 393.757), Orientation = Vector3.new(-15, -45, 0)},
	camera_4 = {Position = Vector3.new(23.407, 405.523, -265.242), Orientation = Vector3.new(-15, -165, 0)},
	camera_5 = {Position = Vector3.new(259.52, 405.523, -193.285), Orientation = Vector3.new(-30, 135, 0)},
	camera_6 = {Position = Vector3.new(-257.155, 392.523, -130.02), Orientation = Vector3.new(-15, -90, 0)},
}

local function cycleCameraView()
	local cameraKeys = {"camera_1", "camera_2", "camera_3", "camera_4", "camera_5", "camera_6"}

	-- Summoned on a separate thread
	task.spawn(function()
		-- Summons a pcall to avoid error swallowing
		local success, err = pcall(function()
			-- Repeatedly runs the code while the loading is running
			while not HAS_LOADING_COMPLETED do
				-- Fires a repeat loop to avoid camera mishaps
				repeat
					local cameraIndex = math.random(1, 6) -- Set as 6 to avoid interval returning as empty
					local key = cameraKeys[cameraIndex]
					currentCamera = CAMERAS[key]
					task.wait()
				until currentCamera ~= previousCamera

				-- Sets the previous camera
				local cameraOrientation = currentCamera.Orientation

				previousCamera = currentCamera
				workspaceCamera.CFrame = CFrame.new(currentCamera.Position) * CFrame.fromOrientation(
					math.rad(cameraOrientation.X),
					math.rad(cameraOrientation.Y),
					math.rad(cameraOrientation.Z)
				)

				task.wait(CAMERA_VIEW_UPDATE_INTERVAL)
			end
		end)

		-- Fires a warn if the cycle failed
		if not success then
			warn("Crash reported on cycleCameraView with err: " ..err)
		end
	end)
end

bro why do people overthinking such simple task here in replies :skull:
Also there no need of getting workspace at all
there is a global workspace already

local Camera:Camera = workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Scriptable
Camera:GetPropertyChangedSignal("CameraType"):Wait()
Camera.CameraType = Enum.CameraType.Scriptable