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)
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
script.Parent.ClickDetector.MouseClick:Connect(function()
i = i == #t and 1 or i + 1
workspace.LastSeen.LastSeen.SurfaceGui.TextLabel.Text = t[i]
end)
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.