Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Click function won't open gui, no errors?

So I am working on a game, however I am using Image Buttons, but when I use a script to open another thing with it, it won't open even though everything looks fine and there are no errors.

Here is the original script! All help appreciated!

local players = game:GetService("Players")
local player = players.LocalPlayer
local name = player.Name

local playerGui = player:WaitForChild("PlayerGui")
local gui = playerGui:WaitForChild("Main")
local frame = gui:WaitForChild("MainFrame")
local display = frame:WaitForChild("Player")
local backDis = display:WaitForChild("Background")

local help = frame:WaitForChild("Help")
local openH = help:WaitForChild("Open")
local frameH = gui:WaitForChild("HelpFrame")
local exitH = frameH:WaitForChild("Close")

local rank = frame:WaitForChild("Rank")
local openR = rank:WaitForChild("Open")

local shop = frame:WaitForChild("Shop")
local openS = shop:WaitForChild("Open")


while true do
	wait(0.03)
	display.Text = name
	backDis.Text = name
	wait(0.03)
end

help.MouseButton1Click:connect(function(onClicked)
	wait(0.03)
	if frameH.Visible == false then
		frameH.Visible = true
	else
		frameH.Visible = false
	end
end)
exitH.MouseButton1Click:connect(function(onClicked)
	wait(0.03)
	frameH.Visible = false
end)

1 answer

##Problem

It appears as though your problem is, you're attempting to run events after an endless loop. Unless you break the loop or make the condition true no longer true, the loop will keep going. I discuss the Infinite Loop Wall in my Common Mistakes blog article.


##Solution

The best way to fix this problem is to move the events before the while loop. That way the script can start listening to them, and still continue with the while loop execution.


##Recommendation

For a little bit of a clean code, you can use the not operator to negate a Boolean value, which is what the visibility values use. You can set the visibility to the opposite of its current visibility setting by simply doing frameH.Visiblity = not frameH.Visibility.


##Final Script

local players = game:GetService("Players")
local player = players.LocalPlayer
local name = player.Name

local playerGui = player:WaitForChild("PlayerGui")
local gui = playerGui:WaitForChild("Main")
local frame = gui:WaitForChild("MainFrame")
local display = frame:WaitForChild("Player")
local backDis = display:WaitForChild("Background")

local help = frame:WaitForChild("Help")
local openH = help:WaitForChild("Open")
local frameH = gui:WaitForChild("HelpFrame")
local exitH = frameH:WaitForChild("Close")

local rank = frame:WaitForChild("Rank")
local openR = rank:WaitForChild("Open")

local shop = frame:WaitForChild("Shop")
local openS = shop:WaitForChild("Open")

help.MouseButton1Click:connect(function(onClicked)
    wait(0.03)
    --[[if frameH.Visible == false then
        frameH.Visible = true
    else
        frameH.Visible = false
    end]]
	frameH.Visible = not frameH.Visible
end)
exitH.MouseButton1Click:connect(function(onClicked)
    wait(0.03)
    frameH.Visible = false
end)

while true do
    wait(0.03)
    display.Text = name
    backDis.Text = name
    wait(0.03)
end

######Hopefully this answered your question. If it did, do not forget to hit the accept answer button. If you have any more question, feel free to comment down below.

This discussion is preserved as it was. New answers, comments, and votes are closed.