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

Why is my random text label script not working?

Asked by
i_Rook 18
3 years ago

Hey! I have recently started scripting, so I decided to learn randoms by creating a label script, but i'm lost. What did I do wrong? If its right, should I move the script/make it a local script? (this is for an intro)

local random = math.random(1,2)

while true do
    if random == 1 then
        script.Parent.TextLabel.Text = "Test"
    elseif random == 2 then
        script.Parent.TextLabel.Text = "Text2"

    end
end

Thanks so much!

0
GUIs are locally situated, meaning Server-sided Scripts (commonly referred to as Regular) wouldn't operate under a UI instance that isn't within a globally shared space, such as workspace. Transfer your code into a LocalScript. Ziffixture 6913 — 3y
0
A quick little tip! You're not yielding your loop, which will cause a Script timeout; your Studio will go haywire. Plop a wait() in there while you're at it! Ziffixture 6913 — 3y
0
Your logic is also partially incorrect, you statically allocate the random number before running the loop, meaning you'll only determine a random number once, and redundantly set the same Text perpetually. Ziffixture 6913 — 3y
0
Move line 1 into the loop. Ziffixture 6913 — 3y
0
silence FEAHREN, too many words, let me explain.... ahem.... script.Parent.TextLabel doesn't exist because you aren't crashing. greatneil80 2647 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

if you want the label to continuously change, you will have to put the math.random inside the while True do loop


while true do local random = math.random(1,2) if random == 1 then script.Parent.TextLabel.Text = "Test" elseif random == 2 then script.Parent.TextLabel.Text = "Text2" end end
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Explanation

Make a table and use math.random to select a random string from the table.

Code

local words = {"hi", "sup", "testing"}

script.Parent.Text = math.random(words)

Edit:I forgot to format my code lol

0
And if you want this continuous, stick it in a loop and add a wait(1) at the top. Reply for more info. KadenBloxYT 135 — 3y

Answer this question