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:
Here you go. Two issues with your original script.
You did not loop your random word generation line.
You need to get the player’s GUI. [Good Practice]
You’re waiting 360 seconds
The code below should help you
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
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 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