How would I make a Ui page selector?

  1. Basicly I want to achieve smooth page movement within this ui
    Screenshot 2023-01-08 at 11.02.59 AM (2)

This is the 4th different iteration of code I have tried and now I am kind of assuming I am making a stupid mistake

-- Variables

local ForwardButton = script.Parent:WaitForChild("Forward_Button")

local BackwardButton = script.Parent:WaitForChild("Back_Button")

local TweenService = game:GetService("TweenService")

local MainGui = game.StarterGui.MainGui

local UpgradeFrame = MainGui.Upgrade_Frame

local Page1 = UpgradeFrame:WaitForChild("Page1")

local Page2 = UpgradeFrame:WaitForChild("Page2")

local Page3 = UpgradeFrame:WaitForChild("Page3")

local function PageChanger_1()

Page1.Visible = false

Page2.Visible = true

end

local function PageChanger_2()

Page2.Visible = false

Page3.Visible = true

end

local function PageChanger_3()

ForwardButton.ImageColor3 = Color3.new(154,154,154)

end

-- Forward Button

ForwardButton.Activated:Connect(function()

if Page1.Visible == true then

PageChanger_1()

elseif Page2.Visible == true then

PageChanger_2()

elseif Page3.Visible == true then

PageChanger_3()

end

end)

I have looked through the developer hub, this page and I could not find anything to discern this problem, like I said this is my 4th iteration of this code

Put all the page frames into one Frame. This makes it easier to use in scripts. Additionally, it’s easier, as the sizing and positioning is in the parented frame and the pages just need the size to be {1, 0} {1, 0}.

Your code does not work, because MainGui is not in StarterGui, but in game.Players.LocalPlayer.Somewhere

Try this code:

local ForwardButton = script.Parent:WaitForChild("Forward_Button")
local BackwardButton = script.Parent:WaitForChild("Back_Button")

local TweenService = game:GetService("TweenService")

-- NOT USED local MainGui = script.Parent.Parent.Parent
local UpgradeFrame = script.Parent.Parent
local Pages = UpgradeFrame.Pages:getChildren()

local currentPage = 1

local function ChangePage(increment)
  for _, Page in Pages do
    if not currentPage + increment > #Pages or not currentPage + increment <= 0 then
      currentPage += increment
      if Page.Name = "Page" .. currentPage then
        Page.Visible = true
      else
        Page.Visible = false
      end
    end
  end
end

ForwardButton.MouseButton1Click:Connect(function()
  ChangePage(1)
end)
BackwardButton.MouseButton1Click:Connect(function()
  ChangePage(-1)
end)