Problems with CFrame/Vector3 (attempt to index nil with 'new')

Hello! I am trying to create a door that opens when you click on it with CFrame/Vector3. The issue is that I keep getting an error that says " attempt to index nil with ‘new’ ". Here is my script (this is my first script on my own :sweat_smile:):

Door = script.Parent

DoorHandle = Door.DoorHandle

ClickDectector = Door.ClickDetector

ClickDectector.MouseClick:Connect(function()
	
	local OpenDoorOrientation = Door.Orientation + CFrame.Orientation.new(0, -90, 0)
	local OpenDoorPosition = Door.Position + CFrame.Position.new(-1.652, 0, 1.621)
	local ClosedDoorOriention = Door.Orientation + CFrame.Orientation.new(0, 0, 0)
	local ClosedDoorPosition = Door.Position + CFrame.Position.new(-9.898, 3.604, 28.405)
	
	if OpenDoorOrientation and OpenDoorPosition then
		ClosedDoorOriention = true
		ClosedDoorPosition = true
	elseif ClosedDoorOriention and ClosedDoorPosition then
		OpenDoorOrientation = true
		OpenDoorPosition = true
		end
	end
)

I have looked at other people’s posts about this error but none of them applied to me. I have rewritten my entire script multiple times but I also get a table error (because I removed the .new after the CFrame). Here is the explorer:

image

1 Like

CFrame.Orientation is nil, and so is CFrame.Position. I think you’re just trying to add an orientation and position to Door.Orientation and Door.Position, so this should work:

        local OpenDoorOrientation = Door.Orientation + Vector3.new(0, -90, 0)
	local OpenDoorPosition = Door.Position +Vector3.new(-1.652, 0, 1.621)
	local ClosedDoorOriention = Door.Orientation
	local ClosedDoorPosition = Door.Position + Vector3.new(-9.898, 3.604, 28.405)

1 Like

I’m pretty sure you don’t need CFrame.Position.new you can just use CFrame.

so roblox made it kind of confusing when putting the position and orientation property under cframe in the properties tab, they are actually properties of the basepart Part.Position and Part.Orientation. when changing position or orientation you’d do the following Part.Position = Vector3.new(0,10,0) which will set the position to Y 10. You can also add to the current position by simply adding a + Part.Position += Vector3.new(0,10,0)

Think of CFrames like a shorthand for position and orientation. So instead of defining them separately you can use CFrame.new(position, orientation) where position and orientation are separately Vector3s.
In your code, however, you would just need to change the CFrame.Position.new with Vector3.new and the same with orientation.

1 Like

Last time I tried that it said that I was trying to make a table, but I’ll but the + next to the =

So I just need to put the position and orientation in the same parentheses like you said?

Yes. You can create them as separate variables and put them in, or, use
CFrame.new(Vector3.new(x,y,z), Vector3.new(x,y,z))

So I rewrited the variables to

Door.Orientation + CFrame.new(Vector3.new(0, -90, 0))
Door.Position + CFrame.new(Vector3.new(-1.652, 0, 1.621))

But I got an error that said " invaild argument #1 (CFrame expected, got Vector3) "

try:
Door.CFrame += CFrame.new(-1.652, 0, 1.621, 0, -90, 0)

uhh, look into the cframe library a bit more but to super simplify down the syntax problem, here:

  • CFrame.Orientation doesn’t exist, you might mean CFrame.Rotation which returns a CFrame
  • CFrame.Position returns a Vector3
  • Whenever you use the variables “CFrame” and “Vector3”, they refer to the class libraries (unless manually set but don’t do that because it’s bad practice). .new is a function of the base class and not the values themselves; what you’re attempting to do is basically Vector3.new(0,0,0).new() and I’m sure you can see why that wouldn’t work
  • i wouldn’t recommend using basepart.Orientation or basepart.Rotation for (almost) anything because CFrames are automatically local space while the others are global, which is usually easier to work with

with all that said, CFrame.Orientation.new() and .Position.new don’t exist, use CFrame.Angles instead, and don’t try and use vector3s for your values (in this scenario)
also that other part of your code has problems if you’re going for what you described but I’ll let someone else handle that

2 Likes

Thank you for the info, I tried to do some research about it before but it didn’t make sense. But now I understand how to format it.

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