Hello. I am making a game and in this game there will probably be a lot of doors and if I make changes to these doors I will need to change the script of all the doors. To overcome this I started using colectionservice yesterday and then strange things happened.
local CollectionService = game:GetService("CollectionService")
for _, model in CollectionService:GetTagged("NormalDoor") do
local door = model.Model.Door
local door2 = model.Model.Door1
local TweenService = game:GetService("TweenService")
local TweenInfoS = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local ProximtyPrompt = model.Model.KeycardReader1.ProximityPrompt
local ProximtyPrompt2 = model.Model.KeycardReader2.ProximityPrompt
local door1default = door.CFrame
local door2default = door2.CFrame
local door1open = door1default * CFrame.new(-3.5, 0, 0)
local door2open = door2default * CFrame.new(3.5, 0, 0)
local OpenSFX = model.Model.Door.DoorOpen
local CloseSFX = model.Model.Door.DoorClose
local bool = false
local DoorIsOpen = false
local clearance = {
["05 Keycard"] = true,
["Captain Access Card"] = true,
["Containment Engineer Access Card"] = true,
["Facility Manager Access Card"] = true,
["Guard Access Card"] = true,
["Medkit"] = true
}
function openDoor()
if bool then return end
bool = true
local Door1OpenTween = TweenService:Create(door, TweenInfoS, {CFrame = door1open})
local Door2OpenTween = TweenService:Create(door2, TweenInfoS, {CFrame = door2open})
OpenSFX:Play()
Door1OpenTween:Play()
Door2OpenTween:Play()
task.wait(2)
bool = false
DoorIsOpen = true
end
function closeDoor()
if bool then return end
bool = true
local Door1OpenTween = TweenService:Create(door, TweenInfoS, {CFrame = door1default})
local Door2OpenTween = TweenService:Create(door2, TweenInfoS, {CFrame = door2default})
CloseSFX:Play()
Door1OpenTween:Play()
Door2OpenTween:Play()
task.wait(2)
bool = false
DoorIsOpen = false
end
function PromptTusk(player)
local PlayerBackPack = player.Backpack
local bp = player:FindFirstChild("Backpack")
if bp then
for _, t in ipairs(bp:GetChildren()) do
if t:IsA("Tool") and clearance[t.Name] then
print("var")
return true
else
print("yok")
return false
end
end
end
return false
end
ProximtyPrompt.Triggered:Connect(function(player)
if PromptTusk(player) then
if DoorIsOpen == true then
closeDoor()
elseif DoorIsOpen == false then
openDoor()
end
end
end)
ProximtyPrompt2.Triggered:Connect(function(player)
if PromptTusk(player) then
if DoorIsOpen == true then
closeDoor()
elseif DoorIsOpen == false then
openDoor()
end
end
end)
end
local PlayerBackPack = player.Backpack
local bp = player:FindFirstChild("Backpack")
if bp then
for _, t in ipairs(bp:GetChildren()) do
if t:IsA("Tool") and clearance[t.Name] then
print("var")
return true
else
print("yok")
continue
-- return false
-- instead of returning, you should continue to the next tool, trying to find the right one, the return makes so that the only first tool is being checked
end
end
end
return false
end
I noticed that all your doors share the same Boolean, and your script is not optimized at all.
local CollectionService = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")
local TweenInfoS = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local clearance = {
["05 Keycard"] = true,
["Captain Access Card"] = true,
["Containment Engineer Access Card"] = true,
["Facility Manager Access Card"] = true,
["Guard Access Card"] = true,
["Medkit"] = true
}
for _, model in CollectionService:GetTagged("NormalDoor") do
local door = model.Model.Door
local door2 = model.Model.Door1
local ProximityPrompt = model.Model.KeycardReader1.ProximityPrompt
local ProximityPrompt2 = model.Model.KeycardReader2.ProximityPrompt
local door1default = door.CFrame
local door2default = door2.CFrame
local door1open = door1default * CFrame.new(-3.5, 0, 0)
local door2open = door2default * CFrame.new(3.5, 0, 0)
local OpenSFX = model.Model.Door.DoorOpen
local CloseSFX = model.Model.Door.DoorClose
local bool = {}
local DoorIsOpen = {}
bool[door.Name] = false
DoorIsOpen[door.Name] = false
local function openDoor()
if bool[door.Name] then return end
bool[door.Name] = true
local Door1OpenTween = TweenService:Create(door, TweenInfoS, {CFrame = door1open})
local Door2OpenTween = TweenService:Create(door2, TweenInfoS, {CFrame = door2open})
OpenSFX:Play()
Door1OpenTween:Play()
Door2OpenTween:Play()
task.wait(2)
bool[door.Name] = false
DoorIsOpen[door.Name] = true
end
local function closeDoor()
if bool[door.Name] then return end
bool[door.Name] = true
local Door1CloseTween = TweenService:Create(door, TweenInfoS, {CFrame = door1default})
local Door2CloseTween = TweenService:Create(door2, TweenInfoS, {CFrame = door2default})
CloseSFX:Play()
Door1CloseTween:Play()
Door2CloseTween:Play()
task.wait(2)
bool[door.Name] = false
DoorIsOpen[door.Name] = false
end
local function PromptTask(player)
local bp = player:FindFirstChild("Backpack")
if bp then
for _, t in ipairs(bp:GetChildren()) do
if t:IsA("Tool") and clearance[t.Name] then
return true
end
end
end
return false
end
ProximityPrompt.Triggered:Connect(function(player)
if PromptTask(player) then
if DoorIsOpen[door.Name] then
closeDoor()
else
openDoor()
end
end
end)
ProximityPrompt2.Triggered:Connect(function(player)
if PromptTask(player) then
if DoorIsOpen[door.Name] then
closeDoor()
else
openDoor()
end
end
end)
end