CCTV error - attempt to index nil with 'CFrame'

Hello. I can’t solve problem - Players.ars899.PlayerGui.ScreenGui.CameraHandler:27: attempt to index nil with ‘CFrame’.
My workspace: https://i.imgur.com/FS4Vffh.png
StarterGUI - https://i.imgur.com/vGphtWV.png
My code (27: cam.CFrame = camParts[1].CFrame):

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local cam = workspace.CurrentCamera
local function abc(ins)
	local children = ins:GetChildren()
	table.sort(children,function(c1,c2)
		return c1.Name:lower() < c2.Name:lower()

	end)
	return children
end
local camParts = abc(workspace.Cameras)
local lArrow = script.Parent.LeftArrow
local rArrow = script.Parent.RightArrow
local arrowClickCooldown = false

char:WaitForChild("Humanoid").Seated:connect(function(isS,seat)
	if not isS or seat.name~="CamSeat" then
		script.Parent.LeftArrow.Visible=false
		script.Parent.RightArrow.Visible=false
		cam.CameraType = Enum.CameraType.Custom
		return
	end
	script.Parent.LeftArrow.Visible=true
	script.Parent.RightArrow.Visible=true
	cam.CameraType = Enum.CameraType.Scriptable	
	cam.CFrame = camParts[1].CFrame

	local camnumber=1
	rArrow.MouseButton1Click:connect(function()
		if arrowClickCooldown then
			return
		end
		arrowClickCooldown=true
		if camnumber==#camParts then
			cam.CFrame = camParts[1].CFrame
			camnumber=1
		else
			cam.CFrame = camParts[camnumber + 1].CFrame
			camnumber+=1
		end
		wait(0.1)
		arrowClickCooldown = false
	end)
	lArrow.MouseButton1Click:connect(function()
		if arrowClickCooldown then
			return
		end
		arrowClickCooldown=true
		if camnumber == 1 then
			cam.CFrame = camParts[#camParts].CFrame
			camnumber= #camParts
		else
			cam.CFrame = camParts[camnumber - 1].CFrame
			camnumber-=1
		end
		wait(0.1)
		arrowClickCooldown = false
	end)
end)

Hey there! I’ve ran your code myself, and I believe I know what your issue is.

When you are running the abc(workspace.Cameras) function, it is happening before the Camera parts have loaded into the game, meaning your table is empty.
image

Therefore, I’ve added the following line of code to your script that waits until the game has finished loading, and this has fixed the issue for me. Let me know if it works for you too.

repeat task.wait() until game:IsLoaded()

You’d place that line before you define the camParts variable. Here is an example:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local cam = workspace.CurrentCamera
local function abc(ins)
	local children = ins:GetChildren()
	table.sort(children,function(c1,c2)
		return c1.Name:lower() < c2.Name:lower()

	end)
	return children
end
repeat task.wait() until game:IsLoaded() -- added this line here
local camParts = abc(workspace.Cameras)

This way, all the parts are fully loaded.
image

2 Likes

Thank you. That’s really genius solve.
I checked the table before and what I was getting in the output, but for some reason it wasn’t empty. I must have made a mistake somewhere. Thanks again, friend.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.