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
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.