How do I fix this?

I am trying to make a system in my game where it opens a random window at a random time and if you don’t close it in a random amount of time then you lose. If you close it then the function repeats until you lose. I made this script, and it works but it has an issue that I can’t figure out:
If you do close it, the function is activated but 2 windows open. If you do close them, then 2 open per window closed and so on, which is not an intended effect. This happens even if you lose and close the window after.

local w1 = script.Parent.Window1.Model
local w2 = script.Parent.Window2.Model
local w3 = script.Parent.Window3.Model
local w4 = script.Parent.Window4.Model
local w5 = script.Parent.Window5.Model
local w6 = script.Parent.Window6.Model
local w7 = script.Parent.Window7.Model

lost = false
open = false
opened = false

function Closed()
	opened = false
	open = false
	script.Event2:Fire()
end

function WindowOpen()
	print("worked")
	open = true
	wait(7)
	local n1 = math.random(10,20)
	--print(n1,"seconds")
	wait(1)
	local n2 = math.random(n1+20,50)
	--print(n2,"seconds")

	local n3 = math.random(1,7)

	wait(n1)
	print("open")
	opened = true
	if n3 == 1 then
		w1.Event:Fire()
	elseif n3 == 2 then
		w2.Event:Fire()
	elseif n3 == 3 then
		w3.Event:Fire()
	elseif n3 == 4 then
		w4.Event:Fire()
	elseif n3 == 5 then
		w5.Event:Fire()
	elseif n3 == 6 then
		w6.Event:Fire()
	elseif n3 == 7 then
		w7.Event:Fire()
	end

	w1.Closed.Event:Connect(Closed)
	w2.Closed.Event:Connect(Closed)
	w3.Closed.Event:Connect(Closed)
	w4.Closed.Event:Connect(Closed)
	w5.Closed.Event:Connect(Closed)
	w6.Closed.Event:Connect(Closed)
	w7.Closed.Event:Connect(Closed)
	
	wait(n2)
	if opened == true then
		lost = true
		if n3 == 1 then
			w1.Part.Transparency = 0 -- This is supposed to tell you if you lost
		elseif n3 == 2 then
			w2.Part.Transparency = 0
		elseif n3 == 3 then
			w3.Part.Transparency = 0
		elseif n3 == 4 then
			w4.Part.Transparency = 0
		elseif n3 == 5 then
			w5.Part.Transparency = 0
		elseif n3 == 6 then
			w6.Part.Transparency = 0
		elseif n3 == 7 then
			w7.Part.Transparency = 0
		end
		print("You lose! The open window was window number",n3)
	end
end

function Event()
	if open == false then
		print("should work")
		script.Event.Event:Connect(WindowOpen)
		script.Event:Fire()
	end
end

script.Event2.Event:Connect(Event)
script.Event2:Fire()

I appreciate any help, as I don’t have any idea why this doesn’t work very well, and I imagine that this script could be a little more efficient. Thanks!

You connect a new connection every time the function runs. Try this code:

--https://devforum.roblox.com/t/how-do-i-fix-this/2619024
local Windows = {
    script.Parent.Window1.Model,
    script.Parent.Window2.Model,
    script.Parent.Window3.Model,
    script.Parent.Window4.Model,
    script.Parent.Window5.Model,
    script.Parent.Window6.Model,
    script.Parent.Window7.Model
}

local lost = false
local open = false
local opened = false

function Closed()
	opened = false
	open = false
	script.Event2:Fire()
end

for _, V in ipairs(Windows) do
    V.Closed.Event:Connect(Closed)
end

function WindowOpen()
	print("worked")
	open = true
	task.wait(7)
	local n1 = math.random(10,20)
	--print(n1,"seconds")
	task.wait(1)
	local n2 = math.random(n1+20,50)
	--print(n2,"seconds")

	local n3 = math.random(1,#Windows)

	task.wait(n1)
	print("open")
	opened = true
    Windows[n3].Event:Fire()

	
	task.wait(n2)
	if opened == true then
		lost = true
        Windows[n3].Part.Transparency = 0
		print("You lose! The open window was window number: ",n3)
	end
end

script.Event.Event:Connect(WindowOpen)

function Event()
	if open == false then
		print("should work")
		script.Event:Fire()
	end
end

script.Event2.Event:Connect(Event)
script.Event2:Fire()

I changed the wait functions to task.wait since ‘wait’ is deprecated, and I changed the window variables into a single table.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.