Camera script not working

Okay so i’ve made a camera script after taking inspirations for my Donald duck game and I have a problem.

local camera = game.Workspace.CurrentCamera

function UpdateCamera()
	camera.CFrame = game.Workspace.Cam1.CFrame
	task.wait(3)
	camera.CFrame = game.Workspace.Cam2.CFrame
	
end

game:GetService("RunService").RenderStepped:Connect(UpdateCamera)

It’s supposed to change cams but it’s not working and it only stay on Cam1. Does anyone know how to fix.

1 Like

Make sure the camera’s CameraType is set to Enum.CameraType.Scriptable.

1 Like

well i believe @Avallachi’s solution is part of it. I noticed that your using task.wait() inside of a render step loop, instead you could make a variable called CurrentCameraPos and put it there

local camera = game.Workspace.CurrentCamera
local CurCameraPos

function UpdateCamera()
	if CurCameraPos then
		--check if its scriptable below, or just set it idk
		workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
		workspace.CurrentCamera.CFrame = CurCameraPos.CFrame
	else
		--return the camera back to the client
	end
end

--now change the curcamerapos

game:GetService("RunService").RenderStepped:Connect(UpdateCamera)

CurCameraPos has a red line. The script that I used was in StarterGui

@wowooms what are you trying to use this script for? Are you trying to switch back and forth between both cameras every 3 seconds?

It’s just like rickey rat games. There is a camera with different positions.

I think your issue is the event you are listening to. Try putting your camera code into a loop. Right now your code is being run over 30 times a second and it is setting the camera position to camera 1 over and over.

local camera = workspace.CurrentCamera
local cam1 = workspace:WaitForChild("Camera1")
local cam2 = workspace:WaitForChild("Camera2")

local function UpdateCamera()
	camera.CFrame = cam1.CFrame
	task.wait(3)
	camera.CFrame = cam2.CFrame
	task.wait(3)
end

while true do
	UpdateCamera()
end

Declare variables as references to your instances (circumvents the need to index them within a repetitively executed loop, define your functions locally (when they do not need to be global) as to integrate them into the local environment, this allows for them to be accessed/referenced quicker as you avoid attempting to locate the function from within the global environment table (_G). As has been stated, the “.RenderStepped” event fires every frame and as you’re using “task.wait()” it’s best to use a “while true do” loop instead as that ensures only a single instance of the function is executing at any given time. Finally, a wait/delay will be required between the camera switching from camera2 back to camera1.

Is the 2 Camera’s CFrame properly set? Actually, in Camera Manipulation, we use Parts for setting the Camera’s CFrame, not Cameras. Here’s an example.

local camera = workspace.CurrentCamera
local part1 = workspace:WaitForChild("Part1")
local part2 = workspace:WaitForChild("Part2")

function UpdateCamera()
	camera.CFrame = part1.CFrame
	task.wait(3)
	camera.CFrame = part2.CFrame
	task.wait(3)
end

game:GetService("RunService").RenderStepped:Connect(UpdateCamera)

It didn’t change the camera tho. It’s just stuck on the first one

I don’t know what you’re talking about, this script works for me. It’s located on ‘StarterPlayerScripts’.

what? the original was located on startergui? oh lol i’ll test it rn

You’re connecting that function to render stepped…
It will always update to cam1, wait 3 seconds and update to cam2, but meanwhile, it will already run the function multiple times on the background

The camera is only at part1 and not part2. Like it doesn’t change.

I am not familiar with ‘RenderStepped’ event since I always use ‘BindToRenderStep’, tell me if this will work or not.

local camera = workspace.CurrentCamera
local part1 = workspace:WaitForChild("Part1")
local part2 = workspace:WaitForChild("Part2")
local TOGGLE_NAME = "put anything here"

function UpdateCamera()
	camera.CFrame = part1.CFrame
	task.wait(3)
	camera.CFrame = part2.CFrame
	task.wait(3)
end

game:GetService("RunService"):BindToRenderStepped(TOGGLE_NAME, Enum.RenderPriority.Camera.Value, function(UpdateCamera))

Huh that script is supposed to be in StarterPlayerScript? If it is , nop not working. It doesn’t even show the camera

I really don’t know what you did wrong, but it is working flawlessly for me.

local camera = game.Workspace.CurrentCamera

function UpdateCamera()
    camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = game.Workspace.Cam1.CFrame
	task.wait(3)
	camera.CFrame = game.Workspace.Cam2.CFrame
	
end

game:GetService("RunService").RenderStepped:Connect(UpdateCamera)
--this works but is very simple and you will need more to make a game. Put it in StarterPlayerScripts in a LocalScript*

wait(2) -- to let the game load*
RunService = game:GetService("RunService")

local camera = workspace.CurrentCamera
local part1 = workspace:WaitForChild("Part1") -- cam position, where the camera is*
local part2 = workspace:WaitForChild("Part2") -- cam focus, where the cam is looking*

function UpdateCamera()
camera.CFrame = CFrame.new(part1.Position,part2.Position)
end

 -- CFrame.new(Position,Where it looks), you also can use CFrame.lookAt() for this*

-- use RunService and :BindToRenderStep to update a cam position each frame*
RunService:BindToRenderStep("JustOneName", Enum.RenderPriority.Camera.Value +1,UpdateCamera)

It’s only on Part1 tho and not Part2