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 ):
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:
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)
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.
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