I Cant Seem to Figure Out How to Script a ‘Previous’ and ‘Next’ Button

Sorry for the inconvenience, but currently I am in the process of making a new horror game, and just yesterday I couldn’t quite figure out how to script a ‘previous’ and ‘next’ button. I need these buttons to allow the player to transition from the first page to the second page of the story.

The pages are going to be on a laptop like the game ‘Start Survey?’, but instead of ‘Yes’ and ‘No’ buttons, I need ‘Next’ and ‘Previous’ buttons.

When ever I try to make a script that I think should work, it just doesn’t. I tried multiple different scripts, even resorting to searching a video on it and nothing came up (apart from one that didn’t work).

Here is an image of what I’m trying to get to work:

set a list of each set of questions in chronological order so like:

local surveyQuestions = {
"question1",
"question2",
"question3"
}

and then make a variable that increases and decreases in terms of number when each button is pressed for ex) pseudocode

--on previous click
if not x==1 then
x = x-1
set surveyUI text to surveyQuestions[i]
end

--on next click
if not x==(length of list) then
x = x+1
set surveyUI text to surveyQuestions[i]
end

and then set the text accordingly within those loops, it can be done all in one script within the screenGUI

For the length of the list you can write #listName so you don’t have to edit it every time you add something

1 Like

yes i know i was just running psuedo cuz my middle finger effed up

You could give a script that you tried to use, instead of having us code it for you. We could then point out the mistakes, generally I feel like that way beginners learn more instead of just copy-pasting scripts.

Here is one of my previous ‘Next’ button scripts that didn’t work:

script.Parent.MouseButton1Click:Connect(OnClicked)

function OnClicked()
	if script.Parent.Parent.Frame1.Visible == true then
		script.Parent.Parent.Frame1.Visible = false
		script.Parent.Parent.Frame2.Visible = true
	end
end

Did you get any errors from this script? Is the script a Localscript? Have you tried adding print statements to make sure OnClicked is getting run?

There was no errors in the Localscript, but I haven’t tried to add print statements yet. Let me try that when I hop back on next, also I made this script recently today (it didn’t work):

local pages = {"Page1", "Page2", "Page3"}

local currentPage = 1

local function showPage(currentPage)
end

local Previous = script.Parent.Previous
local Next = script.Parent.Next

Previous.MouseButton1Click:Connect(function()
	currentPage = currentPage - 1
	if currentPage < 1 then
		currentPage = #pages
	end
	showPage(currentPage)
end)

Next.MouseButton1Click:Connect(function()
	currentPage = currentPage + 1
	if currentPage > #pages then
		currentPage = 1
	end
	showPage(currentPage)
end)


showPage(currentPage)

I tried adding print statements, but they did nothing. Any other advice?

It’s been 27 days and I still cant figure out how to script this…

Multiple months now, still cant find a solution. Anyone able to find one now?

Hi, this is a basic script i wrote that can hopefully give you some understanding of how to make working “Previous” and “Next” system

keep in mind this script is just to give basic idea of how Previous and next button system can be made

local Questions =  {
"hi",
"bye"
}

--// basic gui used for testing
local mainscreengui = script.Parent.Parent.Parent:WaitForChild("ScreenGui")
local testbutton = script.Parent.Parent:FindFirstChild("TextButton")


local CurrentPageIndex = 0


local function updatePage()
	CurrentPageIndex = CurrentPageIndex+1
	
	if Questions[CurrentPageIndex] then --// check if page exists
		local questionlol = Questions[CurrentPageIndex] --// page text
		print(questionlol) --// we print the page text 
	end
end

--// to go to previous page we can do CurrentPageIndex = CurrentPageIndex-1

testbutton.Activated:Connect(function()
	updatePage()
end)

--[[  
one of the best way to make this  is by using intvalues and setting up event that gets fired when value changes

for example we could create int value for every player and when value is changed we can automatically update the page in that case we could decrement or increment a value and it would automatically update

Thank you Bura! I will try this later today!