Attempt to index nil with 'CFrame'

Tried reading this post

but still couldn’t seem to understand how to fix this.

I’m just trying to make the users camera follow this part.

LocalScript in StarterGui

ChangeCam.OnClientEvent:Connect(function(player, campart)
	print("tracking "..player.Name)
	cam.CFrame = campart.CFrame
end)

ServerScript in ServerScriptService

local MakePoop = game:GetService("ReplicatedStorage"):WaitForChild("MakePoop")
local ChangeCam = game:GetService("ReplicatedStorage"):WaitForChild("ChangeCam")
local Poop = game:GetService("ReplicatedStorage"):WaitForChild("Poop")

MakePoop.OnServerEvent:Connect(function(player)
	local playersPoop = Poop:Clone()
	playersPoop.Name = tostring(player.Name).."_Poop"
	playersPoop.Parent = game.Workspace
	ChangeCam:FireClient(player, playersPoop.CamPart)
end)

Then I have this script as another LocalScript in StarterGui for the lobby, where it just makes the camera stay still.

local StarterGui = game:GetService("StarterGui")
local tween = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")
local cam = workspace.Camera
local camera1 = workspace.camera1

UIS.ModalEnabled = true
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
repeat wait() until cam.CameraSubject ~= nil
cam.CameraType = Enum.CameraType.Scriptable

cam.CFrame = camera1.CFrame

Any help would be appreciated

The error message “attempt to index nil with ‘CFrame’” means that the variable campart in your ChangeCam RemoteEvent handler function is nil, and therefore does not have a CFrame property to index.

In your MakePoop RemoteEvent handler function, you are passing the playersPoop.CamPart as an argument to the ChangeCam RemoteEvent. However, it seems like the playersPoop object does not have a CamPart child, which is causing the campart variable to be nil.

To fix this error, you need to make sure that the playersPoop object has a child called CamPart that you can use as the argument for the ChangeCam RemoteEvent. You can do this by adding a part to the playersPoop model and renaming it to “CamPart”, or by adding a new part to the model with the name “CamPart”.

Once you have added the CamPart child to the playersPoop object, you should be able to use it as the argument for the ChangeCam RemoteEvent, and the camera should follow the part as expected.

1 Like

image

I already had a part in there for the camera to track.

is this the full script? if it’s not, you don’t have a variable actually pointing to the camera

an image of the error would be infinitely more helpful because it has a traceback and shows where the error is actually happening

either that or camera1 is nil

My mistake

UIS (LocalScript inside StaterGui)

local UIS = game:GetService("UserInputService")
local MakePoop = game:GetService("ReplicatedStorage"):WaitForChild("MakePoop")
local ChangeCam = game:GetService("ReplicatedStorage"):WaitForChild("ChangeCam")
local cam = workspace.Camera
local camera1 = workspace.camera1


UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space then
		print("pooped")
		MakePoop:FireServer()
	end
end)

ChangeCam.OnClientEvent:Connect(function(player, campart)
	print("tracking "..player.Name)
	cam.CFrame = campart.CFrame
end)

Script (Script inside ServerScriptService)

local MakePoop = game:GetService("ReplicatedStorage"):WaitForChild("MakePoop")
local ChangeCam = game:GetService("ReplicatedStorage"):WaitForChild("ChangeCam")
local Poop = game:GetService("ReplicatedStorage"):WaitForChild("Poop")

MakePoop.OnServerEvent:Connect(function(player)
	local playersPoop = Poop:Clone()
	playersPoop.Name = tostring(player.Name).."_Poop"
	playersPoop.Parent = game.Workspace
	ChangeCam:FireClient(player, playersPoop.CamPart)
end)

Camera (LocalScript inside StaterGui)

local StarterGui = game:GetService("StarterGui")
local tween = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")
local cam = workspace.Camera
local camera1 = workspace.camera1

UIS.ModalEnabled = true
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
repeat wait() until cam.CameraSubject ~= nil
cam.CameraType = Enum.CameraType.Scriptable

cam.CFrame = camera1.CFrame


image

Which script is it not working on?

words to fill in the word count minimum

It’s happening at line 18 in the UIS script so I assume it’s the onclientevent chunk
add a debug print (or use the debugger) to see which variable is causing the error

local UIS = game:GetService("UserInputService")
local MakePoop = game:GetService("ReplicatedStorage"):WaitForChild("MakePoop")
local ChangeCam = game:GetService("ReplicatedStorage"):WaitForChild("ChangeCam")
local cam = workspace.Camera
local camera1 = workspace.camera1


UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space then
		print("pooped")
		MakePoop:FireServer()
	end
end)

ChangeCam.OnClientEvent:Connect(function(player, campart)
	print("tracking "..player.Name)
	warn(`DEBUG: Camera is {cam} and CamPart is {campart}`)
	workspace.CurrentCamera.CFrame = campart.CFrame
end)

Take a screenshot of the debug warn here and it’ll show which variable is erroring

Edit: found the issue, remove player from the onclientevent

ChangeCam.OnClientEvent:Connect(function(campart)
	warn(`DEBUG: Camera is {cam} and CamPart is {campart}`)
	workspace.CurrentCamera.CFrame = campart.CFrame
end)

If you want the player variable to stay, you’re missing a part of the FireClient

local MakePoop = game:GetService("ReplicatedStorage"):WaitForChild("MakePoop")
local ChangeCam = game:GetService("ReplicatedStorage"):WaitForChild("ChangeCam")
local Poop = game:GetService("ReplicatedStorage"):WaitForChild("Poop")

MakePoop.OnServerEvent:Connect(function(player)
	local playersPoop = Poop:Clone()
	playersPoop.Name = tostring(player.Name).."_Poop"
	playersPoop.Parent = game.Workspace
	ChangeCam:FireClient(player, player, playersPoop.CamPart)
end)
1 Like

Thank you!!!

One last thing though, how would I make it repeatedly follow the part, because its a moving part.
Right now it just shows where it was when the space bar was clicked.
Would I just use a while wait(0.1) do or a repeat?

there’s quite a few ways to do it, but since you’re manipulating the camera, I’d say do a RenderStepped loop or bind it to renderstep

1 Like

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