How do I make a question that I wait 5 seconds to complete?

So, I’m making a quiz game sort of like the impossible quiz and I’m trying to make a question that you wait five seconds to move on to the next question. but I made a script, and it doesn’t seem to work.

local currentQuestion = script.Parent
local nextQuestion = currentQuestion.Parent.Question39

if currentQuestion.Visible == true then
	
	wait(5)
	
currentQuestion.Visible = false
nextQuestion.Visible = true
end

This script is inside a frame Called Question38

Setting up an event that detects when the property changes should work. Your current code doesn’t detect changes so nothing will happen.

Fix:

local currentQuestion = script.Parent 
local nextQuestion = currentQuestion.Parent.Question39 
currentQuestion:GetPropertyChangedSignal("Visible"):Once(function(...: any) 
	task.wait(5)
	
	currentQuestion.Visible = false
	nextQuestion.Visible = true
end)

Edit: The code assumes the current question is not visible initially btw

i tried and it did not work but thanks

ScreenGuis don’t have Visible as a valid property. To enable or disable a ScreenGui, you have to use the Enabled property instead:

local currentQuestion = script.Parent
local nextQuestion = currentQuestion.Parent.Question39

currentQuestion:GetPropertyChangedSignal("Enabled"):Connect(function()
	if currentQuestion.Enabled == true then
		task.wait(5)

		currentQuestion.Enabled = false
		nextQuestion.Enabled = true -- assuming this is a ScreenGui as well, correct me if I'm wrong
	end
end

it’s in a frame called Question38
edit: i changed the the description to make it easier to understand

1 Like

Are they both frames that are invisible In the beginning?

yes its Question 38 and Question 39

My code that I posted should’ve worked then but I guess we have to debug first.
Run this code and tell me it prints correctly:

local currentQuestion = script.Parent 
local nextQuestion = currentQuestion.Parent.Question39 
currentQuestion:GetPropertyChangedSignal("Visible"):Connect(function(...: any) 
	print(currentQuestion.Visible)
end)

nothing printed for some reason
edit: it actually did print false

it printed when I clicked one of the options

Run the code again and see if it works again maybe? I remove the delay so you can see instant changes

local currentQuestion = script.Parent 
local nextQuestion = currentQuestion.Parent.Question39 

currentQuestion:GetPropertyChangedSignal("Visible"):Connect(function(...: any) 
	currentQuestion.Visible = not currentQuestion.Visible
	nextQuestion.Visible = not nextQuestion.Visible
end)

I have an error

Maximum event re-entrancy depth (80) exceeded for Object.VisibleChanged

to fix that error you either use debounce or disconnect the connection.

an example with the debounce should look like

local currentQuestion = script.Parent 
local nextQuestion = currentQuestion.Parent.Question39 

local db = false

currentQuestion:GetPropertyChangedSignal("Visible"):Connect(function(...: any) 
if db == true then return end
    db = true -- debounce
	currentQuestion.Visible = not currentQuestion.Visible
	nextQuestion.Visible = not nextQuestion.Visible

    task.delay(1, function() -- setting it back to false
      db = false
    end)
end)

There’s an infinite loop of the event firing and the property changing but @jaybeemvoid’s modification of my code should work

the error is fixed but the script only runs when I click three options I just want it to have a delay of 5 seconds when the question pops up

in that case i suppose you can just change the number in the delay function

 task.delay(seconds, function() -- change seconds to the desired number
    db = false
 end)

works better but for some reason the delay only starts when I hit an option first

it only starts when you hit an option first because the code listens to the “Visible” property and when it is changed it will run the code

is there a way to make the script trigger when you enter the question

Just add a wait function before setting it.

llocal currentQuestion = script.Parent 
local nextQuestion = currentQuestion.Parent.Question39 

local db = false

currentQuestion:GetPropertyChangedSignal("Visible"):Connect(function(...: any) 
	if db then return end
	db = true -- debounce
	
	task.wait(5)
	currentQuestion.Visible = not currentQuestion.Visible
	nextQuestion.Visible = not nextQuestion.Visible

	task.wait(.5)
	db = false
end)

Edit: Don’t create new threads so use the updated code.

1 Like