Random words generator error

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i want to make a working random words generator
  2. What is the issue? Include screenshots / videos if possible!
    i made a random words generator and it keep printing same words
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i searched it on devforum and i cant find solution
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- local words = {
"words",
"pig",
}
local words2 = math.random(#words)
local random = words[words2]

while true do
	wait(1)
	print(random)
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

math.random() requires 2 parameters which are the minimum number and maximum number and you are only providing one number.

You should do this instead:

local words2 = math.random(1,#words)
1 Like
local words = {
"words",
"pig",
}

while true do
        local words2 = math.random(#words)
local random = words[words2]
	wait(1)
	print(random)
end

You have to define the variable inside the loop or else the value will stay constant, but within the loop the variable will change every time.

2 Likes

i tried it before but not working

i tried it and that does work but Is there a way to prevent the same word from being printed? like words,pig,words,pig

All you have to do for that is remove the randomly generated word from the array, so

--: Before wait(1)
words[words2] = nil
1 Like

Yes, you can remove the value from the table.

local words = {
"words",
"pig",
}

while true do
    local words2 = math.random(#words)
    local random = words[words2]
    table.remove(words, words2)
	wait(1)
	print(random)
end
2 Likes

it say attempt to concatenate string with nil

Can you show me what line this error is on?

1 Like
print("Random words : "..randomwords, attemps.Value.."attemps")

Have you defined the variable randomwords?

1 Like

local words2 = math.random(#words)
local randomwords = words[words2]
table.remove(words, words2)

That is because you are removing values from the table so the table would be empty after 2 runs so if you want to keep the value then create a variable that stores the old word,

here is how you do it:

local words = {
"words",
"pig",
}

local Oldword

while true do
    repeat words2 = math.random(1,#words) until words2 ~= Oldword
    local random = words[words2]
   Oldword = random
 	wait(1)
	print(random)
end
1 Like
local words = {
"words",
"pig",
}

while true do
    local words2 = math.random(#words)
    local randomwords = words[words2]
    table.remove(words, words2)
	wait(1)
    if randomwords ~= nil then
        print("Random words : "..randomwords, attemps.Value.."attemps")
    end
end

try this

1 Like

ok i wll try this right now wait soon

thats working wow!!!

it say invaild argument #1 to random but thank you for responsing

i tried it again and it doesnt work

may I know what error are you getting?

there is no error, but it printed “words” twice or three times and then print “pig”