Door script issue

I was having an issue with my door before but it got fixed (Cframe script error message - #3 by JmPopYT) I am now working on my game once again as I had some irl stuff I was busy with. I am duplicating the doors so I can finish the hallway for the building I am creating. When I click E to open a door it now opens a different door. I am not sure what the issue is as I am not a scripter.

This is how the model/script looks in the explorer tab:
image

This is the script:

local parent = script.Parent
local child = script.Parent.DoorHitbox.ProximityPrompt
local doorHitbox = game.Workspace.Door.DoorHitbox
local doorModel = doorHitbox.DoorModel

local function onTriggered(player)
	child.Enabled = false
	for _, part in ipairs(doorHitbox:GetDescendants()) do
		if part:IsA("BasePart") then
			part.CanCollide = false
		end
	end
	for _, part in ipairs(doorModel:GetDescendants()) do
		if part:IsA("BasePart") then
			part.CanCollide = false
		end
	end
	for _, part in ipairs(parent:GetChildren()) do
		if part:IsA("BasePart") then
			part.CanCollide = false
		end
	end
	for i = 1, 7 do
		doorModel:PivotTo(doorModel.PrimaryPart.CFrame - Vector3.new(0.5,0,0))
		wait(0.1)
	end
	wait(1)
	for i = 1, 7 do
		doorModel:PivotTo(doorModel.PrimaryPart.CFrame + Vector3.new(0.5,0,0))
		wait(0.1)
	end
	for _, part in ipairs(doorHitbox:GetDescendants()) do
		if part:IsA("BasePart") then
			part.CanCollide = true
		end
	end
	for _, part in ipairs(doorModel:GetDescendants()) do
		if part:IsA("BasePart") then
			part.CanCollide = true
		end
	end
	for _, part in ipairs(parent:GetChildren()) do
		if part:IsA("BasePart") then
			part.CanCollide = true
		end
	end
	child.Enabled = true
end

child.Triggered:Connect(onTriggered)

Are they all named “Door”? If so, you need to define them using the scripts parent. Or if you want a better system, use CollectionService.

Yes, they’re all named “Door”. How would I go about defining them using the scripts parent? Before I duplicated them the doors did work. I would use CollectionService but I have no clue how to change the script to set that up

Right now you are getting the door by game.workspace.Door, but every door is named “Door.” Thus, your error of it opening only a singular door. Doing script.Parent to find the door would be better because it gets that specific door.

1 Like

When I change “game.workspace.Door” to script.Parent and test out the script it gives me this error: DoorModel is not a valid member of Model “Workspace.Door”

Try “parent.DoorHitbox.DoorModel”

script.Parent only should give the actual Door model. Make a new variable if you need to get something else in the door.

Also, why would you do multiple for loops to make the door collide/not collide when you can just use

for _, part in ipairs(parent:GetDescendants()) do
      if part:IsA("BasePart") then
			part.CanCollide = true --or false
		end
end

I tried “parent.DoorHitbox.DoorModel” and it is still opening another door. Also, I never fully made the script, the parts that I made was all broken and didn’t work. I had to make a devforum post to get help about fixing it which got A door to work now that I have multiple it isn’t working anymore.

I seemed to have gotten the script to work

This is the new working script:

local parent = script.Parent
local child = script.Parent.DoorHitbox.ProximityPrompt
local doorHitbox = script.Parent.DoorHitbox
local doorModel = doorHitbox.DoorModel

local function onTriggered(player)
	child.Enabled = false
	for _, part in ipairs(doorHitbox:GetDescendants()) do
		if part:IsA("BasePart") then
			part.CanCollide = false
		end
	end
	for _, part in ipairs(doorModel:GetDescendants()) do
		if part:IsA("BasePart") then
			part.CanCollide = false
		end
	end
	for _, part in ipairs(parent:GetChildren()) do
		if part:IsA("BasePart") then
			part.CanCollide = false
		end
	end
	for i = 1, 7 do
		doorModel:PivotTo(doorModel.PrimaryPart.CFrame - Vector3.new(0.5,0,0))
		wait(0.1)
	end
	wait(1)
	for i = 1, 7 do
		doorModel:PivotTo(doorModel.PrimaryPart.CFrame + Vector3.new(0.5,0,0))
		wait(0.1)
	end
	for _, part in ipairs(doorHitbox:GetDescendants()) do
		if part:IsA("BasePart") then
			part.CanCollide = true
		end
	end
	for _, part in ipairs(doorModel:GetDescendants()) do
		if part:IsA("BasePart") then
			part.CanCollide = true
		end
	end
	for _, part in ipairs(parent:GetChildren()) do
		if part:IsA("BasePart") then
			part.CanCollide = true
		end
	end
	child.Enabled = true
end

child.Triggered:Connect(onTriggered)

I think you should replace it by

Just replaced it and it seems to be working. What is the main difference with the replacement other then to make the script shorter? Or is that the only difference?

The script is more readable and faster

I just replaced every script with the new one and it seems that the “Hitbox” part isn’t changing CanCollide

Wait if there’s a hitbox part, why do a for loop at all? If the hitbox is the only to collide just change its can collide

If the door isn’t open the CanCollide should be on which is by default, once the door is open all the parts including the “DoorHitbox” should go CanCollide false so people can walk through the door once its open
image

But my point is that a hitbox should be the only thing to collide. So just change the canCollide of the hibox

Even after putting CanCollide off on the Hitbox part I still cant walk through the opened door

yeah that’s maybe because the other parts have can collide too

Even after setting all the “DoorHitbox” CanCollide off I am still unable to walk through opened doors