CollectionService behaving weirdly

I’m using the Tag Editor Plugin to create a global script so that I can operate multiple doors in a simple and efficient manner. I created a script in ServerScriptStorage which successfully opens a door in the Workspace. However, once I add multiple doors (all have the same tag), if you attempt to open one of the other doors it will always default to opening the same door.

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

local taggedParts = CollectionService:GetTagged("SC1+")

for _, taggedDoor in pairs (taggedParts) do
   
   local TweenService = game:GetService("TweenService")

   local door = taggedDoor
   local light1 = taggedDoor.Parent.lightBlock1
   local light2 = taggedDoor.Parent.lightBlock2
   local deniedSound = taggedDoor.accessDenied
   local acceptedSound = taggedDoor.accessGranted
   local doorSound = taggedDoor.doorSound
   local active = false
   local inactive = true
   local prompt1 = door.Parent.lightBlock1.Attachment1.ProximityPrompt
   local prompt2 = door.Parent.lightBlock2.Attachment2.ProximityPrompt

   local tweenInfo = TweenInfo.new(1)
   local openPosition = {}
   local closePosition = {}

   openPosition.CFrame = door.CFrame * CFrame.new(0, 0, (-door.Size.Z*1.1))
   closePosition.CFrame = door.CFrame

   local tweenOpen = TweenService:Create(door, tweenInfo, openPosition)
   local tweenClose = TweenService:Create(door, tweenInfo, closePosition)

   local clearance = {
   	["Master Card"] = true,
   	["Test Card"] = false
   }	

   function accessGranted()
   	inactive = false
   	active = true
   	light1.BrickColor = BrickColor.new("Lime green")
   	light2.BrickColor = BrickColor.new("Lime green")
   	acceptedSound:Play()
   	wait(0.05)
   	doorSound:Play()
   	wait(0.05)
   	tweenOpen:Play()
   	wait(2)
   	doorSound:Play()	
   	wait(0.05)
   	tweenClose:Play()
   	light1.BrickColor = BrickColor.new("Pastel light blue")
   	light2.BrickColor = BrickColor.new("Pastel light blue")		
   	inactive = true
   	active = false		
   end

   function accessDenied()
   	light1.BrickColor = BrickColor.new("Really red")
   	light2.BrickColor = BrickColor.new("Really red")
   	deniedSound:Play()
   	wait(2)
   	light1.BrickColor = BrickColor.new("Pastel light blue")
   	light2.BrickColor = BrickColor.new("Pastel light blue")	
   end

   local function doorthing(Player)
   	if not (Player.Character) then return end

   	local tool = Player.Character:FindFirstChildOfClass("Tool")

   	if not (tool) then return end

   	if clearance[tool.Name] and inactive == true then
   		accessGranted()
   	elseif not clearance[tool.Name] then
   		accessDenied()
   	end
   end
   prompt1.Triggered:Connect(doorthing)
   prompt2.Triggered:Connect(doorthing)
end

Here’s a GIF of what it looks like: https://gyazo.com/f42eda38b7a020eaca19c1094060524f

Make accessDenied() and accessGranted() local functions, my theory is that it sets the global function and uses the scope of the second door.

1 Like