Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

3 way if without elses and elseifs?

I am making a button that will switch between 3 different things when you click on it. For 2 ways, it would be

something.Text = something.Text == "this text" and "other text" or "this text"

but I cannot figure out 3 ways. Any nerds know this?

1 comment

0

So did you look at my answer? Was that what you were looking for?

2 answers

✓ Accepted answer

You can add an additional short circuit. I would recommend using an if statement though, they aren't bad at all.

local a = 3
print( a == 1 and "b" or (a == 2 and "c" or "d") )

2 comments

1

Yep, short circuits are certainly convenient and might look nicer but if statements are way more readable.

0

It matters what if statements, for example elseifs or stacked ifs are never readable, at least for me

If you want it to switch between 3 different text at random then you could create a table and add your 3 texts in it. After creating the table, simply assign the value of a random index of the table to the text using math.random. Example:

local randomStrings = {
"This is my first random string",
"This is my second random string",
"This is my third random string"
}

someting.Text = randomString[(math.random(1,#randomStrings)]

If in the other hand, you have a specific order in which you want to switch the texts from then you could once again, create a table and insert all your strings in it. This time around, you will create a variable which will save the index of the table of the last text shown and show the next index of the table next time you click the button.

local myStrings= {
"This is my first string",
"This is my second string",
"This is my third string"
}
local currentIndex = 0

local function ShowText()
currentIndex = currentIndex +1

--Check if the current index is higher than the amount of indexes in the table. If it is, then revert it back to one.
if currentIndex > #myStrings then
currentIndex = 1
end

something.Text = myStrings[currentIndex]
end

script.Parent.MouseButton1Click:Connect(ShowText)

This discussion is preserved as it was. New answers, comments, and votes are closed.