Hello. I have a scripting Question. So I am trying to make a Topic Chooser for a game and I want the topics to be randomized. For example, the Label says "Topic Chosen: A" then after 112 seconds it changes to "Chosen Topic: B". However, I want the order to be completelely randomized. Here is the script I have that changes the text:
repeat script.Parent.Text = "Topic Chosen: A" task.wait(112) script.Parent.Text = "Topic Chosen: B" task.wait(112) script.Parent.Text = "Topic Chosen: C" task.wait(112) script.Parent.Text = "Topic Chosen: D" task.wait(112) script.Parent.Text = "Topic Chosen: E" task.wait(112) script.Parent.Text = "Topic Chosen: F" task.wait(112) script.Parent.Text = "Topic Chosen: G" task.wait(112) script.Parent.Text = "Topic Chosen: H" task.wait(112) script.Parent.Text = "Topic Chosen: I" task.wait(112) script.Parent.Text = "Topic Chosen: J" task.wait(112) script.Parent.Text = "Topic Chosen: K" task.wait(112) script.Parent.Text = "Topic Chosen: L" task.wait(112) script.Parent.Text = "Topic Chosen: M" until false
A previous answer had you using ascii, however, I'm assuming you want to be able to change the topics to things other than just letters so
I would start by adding all my topics into a table:
local topics = { "A"; "B"; "C"; }
Then I would make a loop that uses the table to choose a random string:
local topics = { "A"; "B"; "C"; } -- Change each of these strings to be a topic you want (i.e. apples, bananas, grapes, etc.) while true do local string = "Chosen Topic: " -- starts out your string variable with the start text that will be the same across each topic local num = math.random(1, #topics) -- #topics will be the number of entries into your topic table, in this instance 3 (A, B & C) string = string..topics[num] -- using the random number generated previously you can reference the table to take that topic script.Parent.Text = string -- set the gui text task.wait(112) -- wait however long you wish end
I'm not the best with explanations, so I'm sorry about that.
I broke the script down into multiple lines so it's easier to see, however, I would just write this as my final script:
local topics = { "A"; "B"; "C"; } while true do script.Parent.Text = "Chosen Topic: "..table[math.Random(1, #table)] task.wait(112) -- wait however long you wish end
You can get a random letter by getting a random ASCII number.
If you don't know what ASCII is, think of it like User IDs. Each player has its own User ID. The same thing applies to letters, numbers, symbols, etc. Everything that you see on your keyboard has its own ASCII Code.
For example, if we translate lowercase a to ASCII using string.byte()
, it will return the number 97 which is the same in this image. To translate it back to lowercase a, you can use string.char()
.
Now that we know what ASCII is, we can now get a random letter using it. First, we will create a variable min
and we'll set it to the ASCII of uppercase A. Another variable max
and that'll be the ASCII of uppercase Z.
local min = string.byte("A") -- A is minimum because this is the first letter of the alphabet local max = string.byte("Z") -- Z is the maximum because this is the last letter of the alphabet
To get a random ASCII number from A to Z we can use either math.random(min, max)
or Random.new():NextInteger(min, max)
. For this one we will use Random.new():NextInteger()
because it has a seed parameter which we can use to make it more accurately random.
local min = string.byte("A") local max = string.byte("Z") local randomASCII = Random.new(tick()):NextInteger(min, max) -- tick() is the number of seconds that have elapsed since UNIX epoch (January 1, 1970 12:00 AM) on the client's device
Now that we got the ASCII Code of the random ltter we can now translate it to a normal letter.
local randomLetter = string.char(randomASCII)
Your script should look like this:
local textLabel = script.Parent local min = string.byte("A") local max = string.byte("Z") while true do -- does the same thing with `repeat until false` local randomASCII = Random.new(tick()):NextInteger(min, max) local randomLetter = string.char(randomASCII) textLabel.Text = "Topic Chosen: " .. randomLetter task.wait(112) end