Attempt to index 'nil' with CFrame bug

Hello I’m trying to make a main menu but in this main menu there is a camera part and I’m trying to make it so that when a player presses a button the camera switches to another camera. However when I do that the camera doesn’t switch and instead gives me this “Attempt to index nil with CFrame” bug.

Here’s my code:

script.Parent.Activated:Connect(function()
	local wait_time = 0.001
	local CurrentCamera = workspace.CurrentCamera
	local part = workspace:WaitForChild("CharacterCreateCamera", wait_time)
	wait(0.001)
	CurrentCamera.CameraType = Enum.CameraType.Scriptable
	CurrentCamera.CFrame = part.CFrame
	game.Lighting.TimeOfDay = 6
	game.Players.LocalPlayer.PlayerGui.ScreenGui.Main.SelectSave.Main.Visible = false
	game.Players.LocalPlayer.PlayerGui.ScreenGui.Main.ButtonHolders.Visible = false
end)

Any idea’s on how to fix this issue? According to the output, the bug is coming from Line 7.
Also, I’m not amazing with scripting, I’m just a beginner.

What is CharacterCreateCamera? Maybe it doesn’t exist or isn’t anchored?

1 Like

part might not exist, so check if it’s not nil by doing this

if not part then
    ...
end
2 Likes

The CharacterCreateCamera is the Camera Part. I just checked and it’s not anchored, allow me a moment to test and see if anchoring it changes anything.

1 Like

Nope, after anchoring the CharacterCreateCamera part it doesn’t seem to help. I’m getting the same output as before.

1 Like

Do you see the part in your in-game explorer when doing a studio test?

1 Like

@bg9

No I do not. How should I fix this?

@DataSigh

After putting in the code snippet you sent, the error disappears but the camera does not switch to the CharacterCreateCamera as it’s intended to.

1 Like

If you’ve made sure it’s anchored try making sure it’s near spawn so that it’s streamed in

1 Like

It’s probably because the part just doesn’t exist. First make sure it exists

1 Like

Well what I’m trying to do is switch the camera’s to show different “zones” I guess. In doing so, I would need to need quite a bit of room to make each zone, so each zone is spread out. Let me show you:


The area above is where the main area for the game is, where the main camera part is.

Below, is the area where the camera is supposed to switch to, the one on the right.

Is there a way to stream it in if it’s at a distance like it is now?

1 Like

How would I go about doing this?

1 Like

First teleport the player to the area where the camera is (because of streaming) then try again

1 Like

You can use Player:RequestStreamAroundAsync Player | Documentation - Roblox Creator Hub on the server then fire a remote to the client when the area is ready

1 Like

I’m not quite sure how to go about doing this, as I’ve never used remote events.

1 Like

I’ve adapted my code to look like this:

script.Parent.Activated:Connect(function()
	local wait_time = 0.001
	local CurrentCamera = workspace.CurrentCamera
	local part = workspace:WaitForChild("CharacterCreateCamera", wait_time)
	wait(0.001)
	CurrentCamera.CameraType = Enum.CameraType.Scriptable
	CurrentCamera.CFrame = part.CFrame
	game.Lighting.TimeOfDay = 6
	game.Players.LocalPlayer.PlayerGui.ScreenGui.Main.SelectSave.Main.Visible = false
	game.Players.LocalPlayer.PlayerGui.ScreenGui.Main.ButtonHolders.Visible = false
	game.Players.LocalPlayer.RequestStreamAroundAsync(Vector3.new(174.94, 78.1, -1030.9))
end)

I’m not quite sure if this is how it’s supposed to look with the RequestStreamAroundAsync thing, however, I’m still getting the attempt to index nil error on Line 7

1 Like

first teleport the player to the spot where the part is because right now it’s just not streamed in, so the reference is nil. Do this BEFORE you use waitforchild

1 Like

Create ReplicatedStorage → StartCamera (Remote Function)

image

Server script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local cameraFunction = ReplicatedStorage.StartCamera

cameraFunction.OnServerInvoke = function(player)
	player:RequestStreamAroundAsync(workspace.CharacterCreateCamera.Position)
	return true
end

Client script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Lighting = game:GetService("Lighting")

local localPlayer = Players.LocalPlayer
local cameraFunction = ReplicatedStorage:WaitForChild("StartCamera")
local mainGui = localPlayer.PlayerGui.ScreenGui.Main
local currentCamera = workspace.CurrentCamera

script.Parent.Activated:Connect(function()
	cameraFunction:InvokeServer()
	
	local part = workspace:WaitForChild("CharacterCreateCamera")
	currentCamera.CFrame = part.CFrame
	currentCamera.CameraType = Enum.CameraType.Scriptable
	Lighting.TimeOfDay = 6
	mainGui.SelectSave.Main.Visible = false
	mainGui.ButtonHolders.Visible = false
end)
3 Likes

I’ve put this in the game, and it does not seem to work. The camera doesn’t switch, but there are no errors this time.

1 Like

Where did you put each script? can you screenshot your explorer

2 Likes

image

I put the server script in the Script in ReplicatedStorage and the Client script in the CharacterCreateCamera local script in the SaveSlot1 Text button

1 Like