Make table keep going after 2nd example

hello! i am working on making it so when you press a button a surfaceGui text label changes to whatever is next. the only problem is that it stops on the 2nd example. quote ([2] = "example2", this)

my current code:

local table = { 
	[1] = "exmaple",
	[2] = "example2",
	[3] = "example3"
	workspace.LastSeen.LastSeen.SurfaceGui.TextLabel.Text = table[2]

how can i make this so when you press the button it goes to another table item? there will be many of them once completed

thanks! :cowboy_hat_face:

You could try it like this:

Just add a variable and everytime you click it you increase the variable by 1.

local table = {
[1] = “exmaple”,
[2] = “example2”,
[3] = “example3”

local index = 1

index = index + 1
workspace.LastSeen.LastSeen.SurfaceGui.TextLabel.Text = table[index]

1 Like

The solution you’ve marked will eventually throw an error when table[index] becomes nil because there’s nothing next.

Try this:

local t = {"example1", "example2", "example3"}
local i = 0

    i = i == #t and 1 or i + 1
    workspace.LastSeen.LastSeen.SurfaceGui.TextLabel.Text = t[i]

P.S., don’t use built-ins as variable names: if the name of a variable gets highlighted (say, table), you might want to change it if you don’t want to accidentally override Lua globals.
Keep also in mind that for this script you don’t need to write t as a key-value table, because:

{"one", "two", "three"}

is the same as:

{[1] = "one", [2] = "two", [3] = "three"}

It’s good practice to only specify the index when creating a mixed table or a dictionary.