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