How do I make random number or word generator?

Heya everyone. I was just wondering how do I make a random generator for a sentence or a word for my game. I tried searching on youtube but didn’t find what I needed. Does anyone know how to do that?

3 Likes

Make a table with your numbers or words and then use math.random to reference that table. Kind of like this…

local numbers = {1,2,3}
local number = math.random(1, #numbers)
local numberchosen = numbers[number]
print(numberchosen)

EDIT: You can make a table with your sentences and words, but remember to put them in speech marks and if you want to make more, put a comma at the end of the word or sentence. Kind of like this…

local sentences = {
"I have a face!",
"I have a foot!",
"I have teeth!"
}

Good luck!

2 Likes

Here is some simple code that utilizes RNG to select a random string from a table.

local sentences = {
"Random Sentence 1",
"Random Sentence 2",
"Random Sentence 3",
"Random Sentence 4",
"Random Sentence 5",
"Etc...",
--[[You can then continue to add multiple strings into this table using the table format.]]
}

You can use sentences[math.random(#sentences)] to then select a random string in the table, here is an example printing a random string to the output.

do
print(sentences[math.random(#sentences)]) --//selects a random sentence in the table and prints it to the output.
end
3 Likes

How do I make it generate when a button is clicked tho? Is it just sciprt.Parent.Mousebutton1click:connect(function()

Well you can do the same thing. Lets say you have a click detector.

local numbs = {1,2,3}

script.Parent.MouseClick:Connect(function()
local randomnum = math.random(1, #numbs)
local chosennum = numbs[randomnum]
print(chosennum)
--end stuff--

Hope this helped! :slightly_smiling_face:

2 Likes

Nope still didn’t work, I press the button and it stays the same.

Hmmmmm are there any errors???

It says Error(7,15) Expected ‘end’ (to close ‘function’ at line 3), got

Can you send me your script? I will be able to help you then.

Here it is image

Question, are you using this for a gui or a click detector?

Here is some simple code that shows the logic of how click detectors work, you can refer to this ClickDetector Developer API for more information on Click Detectors.

local ClickDetector = game.Workspace.ButtonPart.ClickDetector --//Locate the part with the ClickDetector, you can change this to the specific button in the workspace.

local sentences = {
	"Random Sentence 1",
	"Random Sentence 2",
	"Random Sentence 3",
	"Random Sentence 4",
	"Random Sentence 5",
	"Etc...",
}

--//Function that runs the RNG sentence chooser once the ClickDetector is activated.
ClickDetector.MouseClick:Connect(function()
	print(sentences[math.random(#sentences)]) --//selects a random sentence in the table and prints it to the output.
end)

Edit: Sorry for the delayed response, I have accidentally mis-formatted my posts on this thread.

I’m using this for a GUI.
30char

O you cant use mouseclick for guis. MouseClick is only for click detectors.

1 Like

Yeah just realized it said MouseClick
I forgot to say its for a GUI.

2 Likes

Tell me if you have any more problems afterwards!

1 Like

With GUI’s, you can utilize MouseButtonClick to achieve this under a GuiButton, here is a quick example of the following if the local script is inside the GuiButton. If you need resources on how to create GuiButtons, Roblox has released a resourceful Developer API on Creating GUI Buttons.

local GuiButton = script.Parent --//Locate the GuiButton, you can change this to the specific button in Gui.
	
local sentences = {
	"Random Sentence 1",
	"Random Sentence 2",
	"Random Sentence 3",
	"Random Sentence 4",
	"Random Sentence 5",
	"Etc...",
}
	
--//Function that runs the RNG sentence chooser once the GuiButton is activated on a Left Mouse click.
--//If you want the button to be activated with a Right Mouse Click instead, replace MouseButton1Click with MouseButton2Click.
GuiButton.MouseButton1Click:Connect(function()
	print(sentences[math.random(#sentences)]) --//selects a random sentence in the table and prints it to the output.
end)
1 Like