Random Text Generating Per Round

Hello, I’m trying to make a game and I want a text label to generate word at random each round, how can I do that? My current code that doesn’t work is:

local words = {
    "Mango";
    "House";
    "Roblox Icon";
    "Roblox Studio Icon";
    "VR Headset";
    "Noob";
    "Flag";
    "Teddie Bear";
    "Bread";
    "Towel";
    "Ancient Book";
    "Study Book";
    "Shovel";
    "Treasure Chest";
    "Sword";
    "Amogus";
    "Gravity Coil";
    "Speed Coil";
    "Boombox";
    "Helicopter";
    "Car";
    "Fork";
    "TV";
    "Game Controller";
    "Pencil";
    "Eraser";
    "Paintbrush";
}

wait(360)
script.Parent.TextLabel.Text = (words[math.random(#words)])

Thanks for reading
PS, this is where the textlabel is located:
image

1 Like

Your script only runs once.

while true do
    script.Parent.TextLabel.Text = (words[math.random(#words)])
    wait(360)
end

Also this is an inefficient way of doing it.
You should make something that detects when the round ends and then generate a random word.

3 Likes

It doesn’t print out onto the text label though…

try using
words[math.random(1, #words)]

2 Likes

It might be because you’re waiting 6 minutes before it actually sets the text.

Try setting the number on this to a less bigger number.

1 Like

Oh yes I fixed it by changing the location of the wait and the print line. It works now! Thanks

1 Like

Here you go. Two issues with your original script.

  1. You did not loop your random word generation line.
  2. You need to get the player’s GUI. [Good Practice]
  • You’re waiting 360 seconds
    The code below should help you
    RNG

Code:

--// Services 
local Players = game:GetService("Players")

--// Player Components
local Player = Players.LocalPlayer -- Get the player
local PlayerGui = Player.PlayerGui -- Get the player Gui

--// GUI Components
local TextLable = PlayerGui.ScreenGui.TextLabel

--// Table

local words = {
    "Mango";
    "House";
    "Roblox Icon";
    "Roblox Studio Icon";
    "VR Headset";
    "Noob";
    "Flag";
    "Teddie Bear";
    "Bread";
    "Towel";
    "Ancient Book";
    "Study Book";
    "Shovel";
    "Treasure Chest";
    "Sword";
    "Amogus";
    "Gravity Coil";
    "Speed Coil";
    "Boombox";
    "Helicopter";
    "Car";
    "Fork";
    "TV";
    "Game Controller";
    "Pencil";
    "Eraser";
    "Paintbrush";
}

--// Run
local Time = 1 -- How long before a new word is generated in seconds

function GenerateRandomWord () -- You need to call this function once
	while wait(Time) do -- Loops every {Time} is seconds to generate a random word.
		local Word = words[math.random(#words)] -- Generate a random word
		TextLable.Text = Word
	end
end

GenerateRandomWord() -- Run the function 
1 Like

it’s a localscript inside of the gui, he doesn’t need to get the playergui
script.parent would work just fine

1 Like

Well the cleanest way to do this wouldn’t be what you’re trying to do. I’ll show you how to do it with events.

So first create a RemoteEvent in ReplicatedStorage, we’ll name this textUpdateEvent.

Next we want to create a script in ServerScriptService and Edit the script you have in MainGui. We will now link the event to the scripts.

In the server script write:

local textUpdateEvent = game.ReplicatedStorage.textUpdateEvent

local dismission = 360

while true do

textUpdateEvent:FireAllClients()

wait(dismission)

end

and now when the event fires, it will send a signal out to the clients to change the text.

And in the Local Script write:

local textUpdateEvent = game.ReplicatedStorage.textUpdateEvent

local words = {

"Mango";

"House";

"Roblox Icon";

"Roblox Studio Icon";

"VR Headset";

"Noob";

"Flag";

"Teddie Bear";

"Bread";

"Towel";

"Ancient Book";

"Study Book";

"Shovel";

"Treasure Chest";

"Sword";

"Amogus";

"Gravity Coil";

"Speed Coil";

"Boombox";

"Helicopter";

"Car";

"Fork";

"TV";

"Game Controller";

"Pencil";

"Eraser";

"Paintbrush";

}

textUpdateEvent.OnClientEvent:Connect(function()

script.Parent.TextLabel.Text = words[math.random(1, #words)]

end)

And that should do it. Obviously you don’t want to use events like this all the time, but this will be useful for later when you make a Game script with Rounds. also here is the documentary on RemoteEvents

2 Likes