Click detector only opening door once

So I made this script that connects all doors’ click detectors in the game to a function that makes them open while the screen is fading to black, then teleports the player to a random room.

The issue is that it only works once. It works really well, the doors opens nicely and it fades in a cool way. But after it does it once, you can’t even click the door, the cursor doesn’t even look like a click detector cursor.

I have tried making it connect the click detectors in a loop, but it just doesn’t work, how could I fix this efficiently?

PS: there aren’t any errors at all, and it prints everything it’s supposed to print

local tweenService = game:GetService("TweenService")

frame = nil
clickDetector = nil
model = nil
Close = nil
Open = nil
opened = nil

function door_function()
	frame.Sound:Play()
	if opened.Value == false then
		opened.Value = true

		tweenService:Create(frame,TweenInfo.new(0.65),{CFrame = Open.CFrame}):Play()
	end
	
	local fade_out = game.Players.LocalPlayer.PlayerGui.main.fade_out
	for i = 1,0,-0.1 do
		fade_out.BackgroundTransparency = i
		wait(0.001)
	end
	wait(1)
	opened.Value = false

	tweenService:Create(frame,TweenInfo.new(0.65),{CFrame = Close.CFrame}):Play()
	
	local room = nil
	local rooms = 0
	for i,v in pairs(workspace.rooms:GetChildren()) do
		if v.Name:match("room") then
			rooms += 1
		end
	end
	
	while room == nil do
		wait(0)
		for i,v in pairs(workspace.rooms:GetChildren()) do
			if v.Name:match("room") then
				if math.random(0, rooms) == 1 then
					room = v
				end
			end
		end
	end
	
	-- ADJUST LIGHTING AND AMBIENCE FOR EACH ROOM
	
	local lighting = game.Lighting
	
	if room.Name == "room1" then
		lighting.Ambient = Color3.fromRGB(193, 193, 193)
		lighting.Brightness = 0
		lighting.OutdoorAmbient = Color3.fromRGB(136, 136, 136)
		lighting.FogColor = Color3.fromRGB(0, 0, 0)
		lighting.FogEnd = 40
		lighting.FogStart = 0
	end
	
	-- END OF ADJUSTING LIGHTING AND AMBIENCE
	
	game.Players.LocalPlayer.Character:MoveTo(room.spawn.Position)
	for i = 0,1,0.1 do
		fade_out.BackgroundTransparency = i
		wait(0.001)
	end
end

while true do
	for i,v in pairs(workspace.rooms:GetChildren()) do
		if v.Name:match("room") then
			frame = v.door.doorpart
			clickDetector = frame:WaitForChild("ClickDetector")
			model = frame.Parent
			Close = v.door:WaitForChild("DoorClose")
			Open = v.door:WaitForChild("DoorOpen")
			opened = v.door:WaitForChild("Opened")

			print("connecting")
			clickDetector.MouseClick:Connect(door_function)
		end
	end
	wait(5)
end
1 Like

Why are you doing two separate loops? (you are doing while true and then a for loop

didn’t fully read your code though

1 Like

He’s simply looping through everything in a model every 5 seconds

Why are these global? I feel your variables are intermingling with the same local variables and your while true loop is getting confused.

1 Like

Thanks, I figure that’s probably it, I’ll mark this as a solution when I have time to test it

1 Like

Yeah, but you can also just check when a child is added (It might be more efficient)

1 Like

true but after solving that problem I realized neither a loop nor a child added event is required, it was just my sloppiness

You asked why, I answered…

char

oh sorry, my bad

char char char