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

How do I make a random TextLabel appear every 5 seconds?

Ok, to start off, I'll show you my explorer:

StarterGui
	Hints --Frame
		TextLabel
		TextLabel2
		TextLabel3
		TextLabel4

How do I write a script where a TextLabel (randomly) appears, stays on the screen for 5 seconds, then dissapears and then another (random) TextLabel appears?

I tried to explain that as best as I could XD

Lets say when you play the game, on your screen is TextLabel2, then 5 seconds later, TextLabel4 becomes Visible and TextLabel2 dissapears. I just need to know how to implement a random event/function/whatever it is , into my game.

Roblox Wiki won't help, or at least I don't understand it.

1 answer

--First, you need the list of things you want to choose from. Let's say that you only have TextLabels in Hints, then GetChildren will work:
local textLabels = game.Players.LocalPlayer.PlayerGui.Hints:GetChildren()
--If you have other children in Hints that you don't want selected from, you might instead do this:
local ch = game.Players.LocalPlayer.PlayerGui.Hints:GetChildren()
local textLabels = {}
for i = 1, #ch do
	if ch[i].Name:find("TextLabel") == 1 then -- or you might say if ch[i].ClassName == "TextLabel" then -- or whatever you want
		table.insert(textLabels, ch[i])
	end
end

--You want a random one, so near the top of *one script* (in your entire place -- ie don't put this in more than one script), put this:
math.randomseed(tick()); math.random()
--That "seeds" the random number generator and throws away the first random value (which, due to how the randomseed is implemented, doesn't change much for similar seeds)
--Now, to choose a random one, you need only do this:
local label = textLabels[math.random(1, #textLabels)]
--To have a loop, do something like this:
while true do
	local label = textLabels[math.random(1, #textLabels)]
	label.Visible = true
	wait(5)
	label.Visible = false
end

If the TextLabels are identical in every way except their Text, you might instead do this:

local textList = {
	"Text1",
	"Text2",
	"etc",
}
local textLabel = game.Players.LocalPlayer.PlayerGui.Hints.TextLabel
while true do
	label.Text = textList[math.random(1, #textList)]
	wait(5)
end

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