How to make more complicated CollectionService doors?

I’m trying to create a more streamlined and optimized script for doors in a large game I’m developing,
using CollectionService, if possible.

I already have a script that works independently of CollectionService, but I’d like to use the CollectionService for ease of access and control, and less scripts. Here’s my unoptimized and non-CollectionService door script, that’d like to convert to let it work with it, but I have no clue how to make the collection service work with it, along with sound. If someone could help optimize it and lower the lines of code, that’d help too.

local handle = script.Parent.Clicker.Interactive
local doorOpen = false
local changingState = false

handle.ClickDetector.MouseClick:Connect(function()

if doorOpen == true and changingState == false then
changingState = true
for i = 1, 14 do
script.Parent.Door1:SetPrimaryPartCFrame(script.Parent.Door1.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(5.8), 0))
script.Parent.Door2:SetPrimaryPartCFrame(script.Parent.Door2.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(-5.8), 0))
wait()
end
script.Parent.Sounds.DoorOpen:Play()
changingState = false
doorOpen = false
elseif changingState == false then
changingState = true
for i = 1, 14 do
script.Parent.Door1:SetPrimaryPartCFrame(script.Parent.Door1.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(-5.8), 0))
script.Parent.Door2:SetPrimaryPartCFrame(script.Parent.Door2.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(5.8), 0))
wait()
end
script.Parent.Sounds.DoorClose:Play()
changingState = false
doorOpen = true
end
end)

I would recommend you use the Tag Editor plugin if you’re not already. Then tag all of your doors so you can loop through them and create the open/close function.

local CollectionService = game:GetService("CollectionService")

for index, door in pairs(CollectionService:GetTagged("YOUR_DOOR_TAG")) do
    -- your code here
end
1 Like

I’ve tried that, I don’t know if its the wrong naming or something, but I cant get it to work.

Have you tagged all the doors? If you haven’t use:

local CollectionService = game:GetService("CollectionService")

CollectionService:AddTag("TagName")

Maybe use a CollectionService tagging plugin if that doesn’t work.

1 Like

I’ve tagged all the doors, I just don’t know how scripting works when using CollectionService.