Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[SOLVED] I have GUI with 6 pages. How can I get the GUI to close when it is on the last page?

Asked by 4 years ago
Edited 4 years ago

I have an InstructionsGui that has 6 pages. there are 2 button named "Next Page" and "Previous Page" that switch between the pages of the GUI. Also, when the GUI is on The 6th page, the "Next Page" button text switches to say "Close". When the GUI is on the 6th page and you click on the "Close" button, I want the GUI to close, but it does not.

I would appreciate any ideas on how to get this local script to work. The local script is placed inside of my InstructionsGui ScreenGui.

--gui references
local InstructionsGui = script.Parent
local HelpGui = script.Parent.Parent.HelpGui
local Page_1 = script.Parent.Frame.Page_1
local Page_2 = script.Parent.Frame.Page_2
local Page_3 = script.Parent.Frame.Page_3
local Page_4 = script.Parent.Frame.Page_4
local Page_5 = script.Parent.Frame.Page_5
local Page_6 = script.Parent.Frame.Page_6

-- button references
local InstructionsButton = script.Parent.Parent.HelpGui.Frame.InstructionsButton
local nextPg = script.Parent.Frame.NextPage
local previousPg = script.Parent.Frame.PreviousPage

--variable to track page number
local PageNum = 0

--function to show instructions
local function showInstructions()
    HelpGui:Destroy()
    InstructionsGui.Enabled = true
    InstructionsGui.Frame.Visible = true
    Page_1.Visible = true 
    PageNum = 1
end

--function to show next page
local function nextPage()
    PageNum = PageNum + 1
    if PageNum == 7 then
        PageNum = 6
    end
    if PageNum < 6 then
        nextPg.Text = "Next Page"
    else if PageNum == 6 then
        nextPg.Text = "Close"
    end
    end
    InstructionsGui.Frame:FindFirstChild("Page_"..PageNum).Visible = true
    InstructionsGui.Frame:FindFirstChild("Page_"..PageNum - 1).Visible = false
end

--function to show previous page
local function previousPage()
    PageNum = PageNum - 1
    if PageNum == 0 then
        PageNum = 1 
    end
    if PageNum < 6 then
        nextPg.Text = "Next Page"
    else if PageNum == 6 then
        nextPg.Text = "Close"
    end
    end
    InstructionsGui.Frame:FindFirstChild("Page_"..PageNum).Visible = true
    InstructionsGui.Frame:FindFirstChild("Page_"..PageNum + 1).Visible = false
end

--function to destroy gui after player is done reading instructions
local function closeInstructions()
InstructionsGui:Destroy()
InstructionsGui.Enabled = false
InstructionsGui.Frame.Visible = false

for i = 1, 6, 1
do
InstructionsGui.Frame:FindFirstChild("Page_"..PageNum):Destroy()
end 
end

--event to show instructions
InstructionsButton.MouseButton1Click:Connect(showInstructions)

--event that flips to the next page, or destroys gui if player clicks close on the last page of instructions 
if PageNum ~= 6 then
    nextPg.MouseButton1Click:Connect(nextPage)
else if PageNum == 6 then
    nextPg.MouseButton1Click:Connect(closeInstructions) 
end 

end

--event that flips the previous page
previousPg.MouseButton1Click:Connect(previousPage)

I think my problem resides in the function below, but I am not sure what to change to get the GUI to close.

--function to destroy gui after player is done reading instructions
local function closeInstructions()
InstructionsGui:Destroy()
InstructionsGui.Enabled = false
InstructionsGui.Frame.Visible = false

for i = 1, 6, 1
do
InstructionsGui.Frame:FindFirstChild("Page_"..PageNum):Destroy()
end 
end
0
Disabling the ScreenGui should be all there is to it. Try removing all of the lines from closeInstructions except for the line where you disable it. Hypgnosis 186 — 4y
0
okay, I'll try that and let you know if it works. alivemaeonman 14 — 4y
0
It's so strange, because i've been messing with the code and nothing seems to work, even your recommendation does not work, i am so lost. alivemaeonman 14 — 4y
0
ok, this might not be the best thing to do, but you should rewrite the code, so then you can have a better understanding of it, I think... Loot_O 42 — 4y
View all comments (2 more)
0
i've been rewriting the functions all day............ alivemaeonman 14 — 4y
0
I have found the solution to my problem, i just made a function called pageRange() and then put nextPg.MouseButton1Click:Connect(closeInstructions) inside and if statement to detect when the GUI is on the last page alivemaeonman 14 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I have solved this question on my own through motivation of the comments. Thanks to those who commented on my post! The new and working code is pasted below if you would like to see the final product.

--gui references
local InstructionsGui = script.Parent
local HelpGui = script.Parent.Parent.HelpGui
local Page_1 = script.Parent.Frame.Page_1
local Page_2 = script.Parent.Frame.Page_2
local Page_3 = script.Parent.Frame.Page_3
local Page_4 = script.Parent.Frame.Page_4
local Page_5 = script.Parent.Frame.Page_5
local Page_6 = script.Parent.Frame.Page_6

-- button references
local InstructionsButton = script.Parent.Parent.HelpGui.Frame.InstructionsButton
local nextPg = script.Parent.Frame.NextPage
local previousPg = script.Parent.Frame.PreviousPage

--variable to track page number
local PageNum = 0

--function to show instructions
local function showInstructions()
    HelpGui:Destroy()
    InstructionsGui.Enabled = true
    InstructionsGui.Frame.Visible = true
    Page_1.Visible = true 
    PageNum = 1
end

--function to disable gui after player is done reading instructions
local function closeInstructions()
    InstructionsGui.Enabled = false
end

--makes sure pages don't go out of range
local function pageRange()
if PageNum < 6 then
        nextPg.Text = "Next Page"
    else if PageNum == 6 then 
        nextPg.Text = "Close"

        --event which closes instructions if "Close" button is clicked
        nextPg.MouseButton1Click:Connect(closeInstructions)
    end
    end 
end

--function to show next page
local function nextPage()
    PageNum = PageNum + 1
    if PageNum == 7 then
        PageNum = 6
    end
    pageRange()
    InstructionsGui.Frame:FindFirstChild("Page_"..PageNum).Visible = true
    InstructionsGui.Frame:FindFirstChild("Page_"..PageNum - 1).Visible = false
end

--function to show previous page
local function previousPage()
    PageNum = PageNum - 1
    if PageNum == 0 then
        PageNum = 1 
    end
    pageRange()
    InstructionsGui.Frame:FindFirstChild("Page_"..PageNum).Visible = true
    InstructionsGui.Frame:FindFirstChild("Page_"..PageNum + 1).Visible = false
end

--event to show instructions
InstructionsButton.MouseButton1Click:Connect(showInstructions)

--event that flips to the previous page
previousPg.MouseButton1Click:Connect(previousPage)

--event that flips to the next page 
nextPg.MouseButton1Click:Connect(nextPage)

All that I had to do was create a separate function to handle which page the GUI is currently on, and I placed the event inside of there.

Ad

Answer this question