GUI PopUp issue using math.random in a Roblox GUI Quiz Game

I’m working on making a local script that functions to when you click a button, the menu disappears and a folder of quiz screens has quiz screens to be randomized in the script. I can’t get the quiz GUI to randomize. I’ve tried looking at videos but there aren’t many I can find.

local button = script.Parent
local QuizScreen1 = button.Parent.Parent.QuizScreens.QuizScreen
local QuizScreen2 = button.Parent.Parent.QuizScreens.QuizScreen2
local QuizScreens = {
	QuizScreen1,
	QuizScreen2
}
button.MouseButton1Click:Connect(function()
	button.Parent.Parent.Enabled = false
	local quiz = QuizScreens[math.random(1, #QuizScreens)]
	quiz.Visible = true
end)

5 Likes

any errors? does it not work ?

It doesn’t work, there is nothing in the output either.

Debug your code with print statements

1 Like

since you said that there is no error, you should try adding a print at the start of the buttonpress event. You should also add a print that print the quiz that is chosen. if you receive non of the prints, means there is something wrong with the event . if you receive both , that means there is something wrong with making the quiz visible. you can check the parent of the quiz and the visible value.by right quiz should not be nil

I debugged it, it all printed. The randomized quiz screen still didn’t show visible though.

Are you sure the quiz screens are correct or they show in studio?

They are all right. I’m trying to play with quiz screens in the explorer because it was originally in a folder but its still not working.

local button = script.Parent
local QuizScreen1 = button.Parent.Parent.QuizScreens.QuizScreen
local QuizScreen2 = button.Parent.Parent.QuizScreens.QuizScreen2
local QuizScreens = {
	QuizScreen1,
	QuizScreen2
}
button.MouseButton1Click:Connect(function()
	button.Parent.Parent.Enabled = false
	local quiz = QuizScreens[math.random(1, #QuizScreens)]
	quiz.Visible = true
    print(quiz:GetFullName()) -- gets path of object
end)

I know the problem actually, I’m unenabling the ScreenGui which is a parent to the quiz screen, which im trying to make visible.

Why are you unenabling it? You could use event:Wait instead

I’m unenabling it so that I don’t see the main menu.

You could just make the main menu not visible

I tried to do it without the ScreenGui shutting it out but its still not working.

your code should be

local button = script.Parent
local QuizScreen1 = button.Parent.Parent.QuizScreens.QuizScreen
local QuizScreen2 = button.Parent.Parent.QuizScreens.QuizScreen2
local QuizScreens = {
	QuizScreen1,
	QuizScreen2
}
button.MouseButton1Click:Connect(function()
	-- button.Parent.Parent.Enabled = false
	local quiz = QuizScreens[math.random(1, #QuizScreens)]
	quiz.Visible = true
end)
local button = script.Parent
local QuizScreen1 = game.StarterGui.OneQuiz:WaitForChild("QuizScreen1")
local QuizScreen2 = game.StarterGui.TwoQuiz:WaitForChild("QuizScreen2")
local QuizScreens = {
	QuizScreen1,
	QuizScreen2
}
button.MouseButton1Click:Connect(function()
	print("Clicked")
	button.Parent.Visible = false
	print("Main Menu Not There")
	task.wait()
	local quiz = QuizScreens[math.random(1, #QuizScreens)]
	task.wait()
	print("Randomized")
	quiz.Visible = true
	print("Visible")
end)

I changed a bit of the explorer since I came up with the theory that the screengui was ruining the boolean values of my script.

You should set the main menu to visible = false and remove setting the gui’s enabled property

I did that, it’s still not working, I think it has something to do with the randomization.

I see your issue. StarterGui is copied and sent to the player’s PlayerGui, so the player doesn’t get the update.

local button = script.Parent
local player = game.Players.LocalPlayer
local PlayerGui = player:WaitForChild("PlayerGui")
local QuizScreen1 = PlayerGui.OneQuiz:WaitForChild("QuizScreen1")
local QuizScreen2 = PlayerGui.TwoQuiz:WaitForChild("QuizScreen2")
local QuizScreens = {
	QuizScreen1,
	QuizScreen2
}
button.MouseButton1Click:Connect(function()
	print("Clicked")
	button.Parent.Visible = false
	print("Main Menu Not There")
	task.wait()
	local quiz = QuizScreens[math.random(1, #QuizScreens)]
	task.wait()
	print("Randomized")
	quiz.Visible = true
	print("Visible")
end)