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

Why is my random text label script not working?

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!

5 comments

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.

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!

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.

0

Move line 1 into the loop.

0

silence FEAHREN, too many words, let me explain.... ahem.... script.Parent.TextLabel doesn't exist because you aren't crashing.

2 answers

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

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

1 comment

0

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

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