Attempt to index nil with CFrame?

Hi, I get the error “Attempt to index nil with CFrame” when running the code. This function runs a few times with other objects and works perfectly.

What is the hierarchy for that object’s PositionParts

1 Like

image

If objectName is a StringValue, it will not have a CFrame property. I recommend changing it to your “object” variable you created to reference the objectName, or somehow get the actual part, instead of the name.

yeah, but I use the objectName to reference an object using FindFirstChilld

Yeah I just realised that after you replied with your hierarchy.

The error is basically saying that objectName+”PositionPart” does not exist, so I would make sure that whatever object you are trying to use for the CFrame exists, and also make sure objectClone exists. I will keep looking at your code to look for other possible issues.

Edit: Before I said that the part does not have a CFrame property, but the error actually means that the part itself does not exist.

Ok thank you! As you can see here, when the game is run the lamp object does exist
image

it really is just strange because it is the same as the other objects

Which objects does it not work with? You mentioned it works with some, but not others.

Instead of going with if not object then return end,
try if object == nil then return end

it does not work with the lamp

ok, I tried that and I get the same error

Can you try replacing :FindFirstChild with :WaitForChild on the line where you set objectClone.CFrame? Maybe the instance doesn’t exist yet.

1 Like

There is no part named ObjectName .. “PositionPart”. Print out ObjectName and see what it’s value is, then check to see if a part with the correct name exists.

I just tried that.

1 Like

Are you able to look inside the folder while the script is running and verify that LampPositionPart does exist?

I found the issue! Lamp position part was unanchored so it fell out the world an deleted

1 Like

I suspected that something like that could’ve occurred. Everything else looked fine, so that was the only probable cause for the issue. Glad you figured it out :happy2:.

1 Like

Maybe the object is a model? you could try checking, and respond accordingly, since models don’t have a CFrame property.
if objectClone:IsA("Model") then objectClone.PrimaryPart.CFrame = ..etc else objectClone.CFrame

Could you also add in some prints to check what’s being passed?
print(typeof(objectClone)), for example.