Trying to make a camera switcher for my menu give me a error

Ok so im trying to make a script where the camera switches to a diffrent part every few seconds for my menu but i cant make it work it keeps giving me a “attempt to call a nil value” error this is my code:

local camera = game.Workspace.CurrentCamera

repeat
		function updateCamera()
	camera.CFrame = game.Workspace:WaitForChild("MenuCamera").CFrame --Makes the camera go to the camera brick
	end
	
	wait(5)
	
	function updateCamera1()
		camera.CFrame = game.Workspace:WaitForChild("MenuCamera1").CFrame --Makes the camera go to the camera brick
	end
until script.Parent.Play.MouseButton1Click:Connect(function()
	camera.CameraType = Enum.CameraType.Custom
end)

also the script.Parent.Play.MouseButton1Click:Connect(function() is for when the player click the “play” button and the script stops running.

What is the error? (30 ______)

1 Like

This is the error and the number keeps going up every milisecond

Hey! So I copied your code and saw some issues:
1.Do you really need the functions updateCamera and updateCamera1? It works fine without them.
2. The changes won’t be visible since it will repeat setting the CFrame to the first one right after, since the loop starts again. So another wait is needed.
3. I put the repeat wait() until game:IsLoaded, because maybe the game won’t load untill then…
4. I am not sure if this is right, but I don’t think you can put untill then give the function a purpose, so I put a variable instead.
So here is the code (I fixed it):

repeat wait() until game:IsLoaded()

local camera = game.Workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable

on = true

script.Parent.Play.MouseButton1Click:Connect(function()
camera.CameraType = Enum.CameraType.Custom
on = false
end)
repeat

	camera.CFrame = game.Workspace:WaitForChild("MenuCamera").CFrame --Makes the 
camera go to the camera brick

    	wait(5)

  camera.CFrame = game.Workspace:WaitForChild("MenuCamera1").CFrame --Makes the camera go to the camera brick>	
wait(5)

until on == false

Also it should be a local script but I guess it already is

1 Like

Which line? (30 _____________)

1 Like

Thanks it works! @JamminRedPandaMan it didnt say but @Fulger59 fixed it!

2 Likes