Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a Label change text in a RANDOMIZED order?

Asked by 1 year ago

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
0
"Repeat until False" makes the script loop forever. Cubispaghettis 2 — 1y

2 answers

Log in to vote
1
Answered by 1 year ago

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

0
That’s literally the same thing with my answer, except it’s shorter and faster. T3_MasterGamer 2189 — 1y
0
oh wait nvm, this is the real correct answer :thumbs_up: T3_MasterGamer 2189 — 1y
Ad
Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

Solution

You can get a random letter by getting a random ASCII number.

What is ASCII?

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().

How do I use it in my script?

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)

Final Script

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
0
This solution would only work if they specifically wanted each topic to be a letter, but I assume that the letters are just place holders for actual topics. MoralArsonism 47 — 1y
0
Yeah theyre placeholders but still helps Cubispaghettis 2 — 1y
0
Then why did he show in the question “Topic Chosen: A” “Topic Chose: B” T3_MasterGamer 2189 — 1y
0
ok T3_MasterGamer 2189 — 1y
View all comments (2 more)
0
What you can do is use MoralArsonism’s answer but change the letter table to the words/topics you want in the game. T3_MasterGamer 2189 — 1y
0
nvm he already said that T3_MasterGamer 2189 — 1y

Answer this question