Slidding doors not working

Hey developers!
Today I tried to make sliding doors, I tried to make a script but it is not working. Could someone help me and tell me what’s wrong. In output it says: ( [Workspace.slidingdoors.Script:13: attempt to index nil with ‘CFrame’), here’s the script:

local Opened = false
local detector = script.Parent.Detector
local leftDoor = script.Parent.LeftDoor
local rightDoor = script.Parent.RightDoor

function OnTouched(hit)
local humanoid = hit.Parent:FindFirstChild(“Humanoid”)
if humanoid then
if Opened == false then
Opened = true
for i = 1, 40 do
rightDoor:SetPrimaryPartCFrame(rightDoor.PrimaryPart.CFrame + rightDoor.PrimaryPart.CFrame.LookVector0.1)
leftDoor:SetPrimaryPartCFrame(leftDoor.PrimaryPart.CFrame - leftDoor.PrimaryPart.CFrame.LookVector
0.1)
wait()
end
wait(Time)
for i = 1, 40 do
rightDoor:SetPrimaryPartCFrame(rightDoor.PrimaryPart.CFrame - rightDoor.PrimaryPart.CFrame.LookVector0.1)
leftDoor:SetPrimaryPartCFrame(leftDoor.PrimaryPart.CFrame + leftDoor.PrimaryPart.CFrame.LookVector
0.1)
wait()
end
Opened = false
end
end
end

detector.Touched:Connect(OnTouched)
please help.

1 Like

Sorry, but please format your script next time by putting it between three backticks ``` like a sandwich so you get a result like below:

local Opened = false
local detector = script.Parent.Detector
local leftDoor = script.Parent.LeftDoor
local rightDoor = script.Parent.RightDoor

function OnTouched(hit)
local humanoid = hit.Parent:FindFirstChild(“Humanoid”)
if humanoid then
if Opened == false then
Opened = true
for i = 1, 40 do
rightDoor:SetPrimaryPartCFrame(rightDoor.PrimaryPart.CFrame + rightDoor.PrimaryPart.CFrame.LookVector0.1)
leftDoor:SetPrimaryPartCFrame(leftDoor.PrimaryPart.CFrame - leftDoor.PrimaryPart.CFrame.LookVector0.1)
wait()
end
wait(Time)
for i = 1, 40 do
rightDoor:SetPrimaryPartCFrame(rightDoor.PrimaryPart.CFrame - rightDoor.PrimaryPart.CFrame.LookVector0.1)
leftDoor:SetPrimaryPartCFrame(leftDoor.PrimaryPart.CFrame + leftDoor.PrimaryPart.CFrame.LookVector0.1)
wait()
end
Opened = false
end
end
end

detector.Touched:Connect(OnTouched)

Anyways for the problem, it seems like the script is not able to access the door base parts, make sure these variables refer to a model with a primary part set for both of them.

local leftDoor = script.Parent.LeftDoor
local rightDoor = script.Parent.RightDoor

This is because the script uses the primary part of the model to obtain a CFrame to rotate it.

rightDoor.PrimaryPart.CFrame
2 Likes

Thanks! :slight_smile:
One question. Now my doors are moving forward and backwards how can I make them move sidewise?

I would replace the this part

Instead of moving the door where it is looking I would move it using it’s .RightVector. You can make this the left vector by multiplying it by -1 which reverses the vector direction.

1 Like

For the next time, I would recommend you to use TweenService, as it is performing and looking way better.