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

How to make random words in localscript?

I created a local script that checks text in textbox and closes frame. Here is:

local TextBox = script.Parent.Parent.Parent.TextBox
local giButton = script.Parent
local frame = script.Parent.Parent.Parent.Parent.Frame
local button = script.Parent.Parent.Parent.Wii.Text


giButton.MouseButton1Click:Connect(function()
	if TextBox.Text == ("Example Word") then
		TextBox.Text = ("")
		frame.Visible = false
		wait (5)
		
	else
		frame.Visible = false
		
	end
end)

Of course, I created a textlabel so that the player could know what to type in the textbox.

Of course, players could copy and paste that word hundreds of times. I decided to make the words random. It means that from a few words I have selected, the script will randomly select one word and then insert it into the textlabel. It will also set it as the desired search result.

Unfortunately, when I tried to do it, either something didn't work or it was very lazy.

Anyone have an idea how this can be done?

1 comment

0

Can you give more explanation what are you trying to achieve

1 answer

This is relatively simple, at least my method. Just try to remember that tables store data based on numbers.

local Words = {
	"Hello",
	"This",
	"Is",
	"The",
	"List",
	"Of",
	"Words"
}
local amountOfWords = 0 -- Place this where it is only set to 0 ONCE! Like at the top of a script.
for i,v in pairs(Words) do
	amountOfWords = amountOfWords + 1
end
local chosenWord = math.random(0,amountOfWords)
local stringWord = Words[chosenWord]
print(stringWord)

I hope the code above helps!

DetectiveRaie, best regards.

2 comments

0

you can do ;; amountOfWords = #Words ;; And lua tables start with 1 so ;; math.random(1 ... ;; not 0

0

and how, for example, when completing an activity, generate a different word?

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