How to create collection service doors?

I’ve been trying to make collection service doors for over an hour but nothing has ever worked, I was planning on using the same method I use with my doors that aren’t collection service but it never let me set the primary part, so the entire script was useless.

I’ve done as much as I know and don’t really know how to go on further with it, but I know I need to make collection service doors or my game will be laggy as heck, I’ve tried using other code but it never worked for me.

Does anyone have old code they used for collection service doors, or can anyone explain it to me?

1 Like

Well I’m more than glad to help! But can you please give us the code you made, so we can build off of it, and explain to you what you did wrong.

1 Like

local CollectionService = game:GetService(“CollectionService”)
local Tagged = CollectionService:GetTagged(“Doors”)
local TweenService = game:GetService(“TweenService”)

for _, Tagged in pairs(Tagged) do
spawn(function()

end)
local backward = Tagged.Door.PrimaryPart.Cframe
local forward = backward*CFrame.new(0,6.3,0)
local CF = Instance.new(“CFrameValue”)
CF.Value = backward
CF.Changed:Connect(function()
Tagged.Door:SetPrimaryPartCFrame(CF.Value)
while true do
wait(.1)
local C = 0
for i,v in pairs(Tagged.Door.Interactive.Interactive()) do
Tagged.Door.Interactive.Interactive.ClickDetector.Mouseclick:Connect(function(clicked)

		if clicked and C == 0 then 
			TweenService:Create(CF,TweenInfo.new(.5),(Value == backward)):Play()
		else
			TweenService:Create(CF,TweenInfo.new(.5),(Value == forward)):Play()
		end
	end
end)

end
This isn’t my original one but It’s close to it.

local CollectionService = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")

local Clicked = {}
local TaggedDoors = CollectionService:GetTagged("Doors")

for _, TaggedDoor in ipairs(TaggedDoors) do
	local Backward = TaggedDoor.PrimaryPart.CFrame
	local Forward = Backward * CFrame.new(0, 6.3, 0)
	local CFrameValue = Instance.new("CFrameValue")
	CFrameValue.Value = Backward
	CFrameValue.Changed:Connect(function(New)
		TaggedDoor.Door:SetPrimaryPartCFrame(New)
	end)
	TaggedDoor.Door.Interactive.Interactive.ClickDetector.MouseClick:Connect(function(Player)
		Clicked[TaggedDoor] = not Clicked[TaggedDoor]
		TweenService:Create(CFrameValue, TweenInfo.new(0.5), {Value = Clicked[TaggedDoor] and Backward or Forward}):Play()
	end)
end

This should work if you have tagged your doors properly, didn’t test this though and obviously there are more efficient ways to achieve your desired result. Also i’d recommend if you’re trying to tween a model to not use this method as SetPrimaryPartCFrame is known for it’s weird behaviour. If you’re looking for a better method i’d recommend checking out colbert’s thread Introduction to Tweening Models

I rewrote the code to better fit my doors, as they open sideways and not vertical (because i used code from somewhere else) and it opens, but it opens a bit too much… Anyways, here’s the code I rewrote from it and that’s making open so much. I slowed the speed of opening to see how it moved.

local CollectionService = game:GetService(“CollectionService”)
local TweenService = game:GetService(“TweenService”)

local Clicked = {}
local Tagged = CollectionService:GetTagged(“Doors”)

for _, TaggedDoor in ipairs(Tagged) do
local Backward = TaggedDoor.PrimaryPart.CFrame
local Forward = Backward * (TaggedDoor.PrimaryPart.CFrame * CFrame.Angles(0,0,0))
local CFrameValue = Instance.new(“CFrameValue”)
CFrameValue.Value = Backward
CFrameValue.Changed:Connect(function(New)
TaggedDoor:SetPrimaryPartCFrame(New)
end)
TaggedDoor.Interactive.Interactive.ClickDetector.MouseClick:Connect(function(Player)
Clicked[TaggedDoor] = not Clicked[TaggedDoor]
TweenService:Create(CFrameValue, TweenInfo.new(5), {Value = Clicked[TaggedDoor] and Forward or Backward}):Play()
end)
end