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

Is this the correct way to get a random value?

On the lines 4-6 are the same error's and I do not know how to fix it. I've been looking around the internet for a solution but I can not seem to get it to work. May I ask what is wrong and if there are any errors in the rest of the script?

local Front = game.ReplicatedStorage.FrontSentence
local Adjective = game.ReplicatedStorage.RandomWord
local Back = game.ReplicatedStorage.BackSentence
local selectedIndex1 = math.random(#Front)
local selectedIndex2 = math.random(#Adjective)
local selectedIndex3 = math.random(#Back)
local one = table.remove(Front, selectedIndex1)
local two = table.remove(Adjective, selectedIndex2)
local three = table.remove(Back, selectedIndex3)
print (one, two, three)

I don't know too much about Lua so I do not really understand the Random values too well as it's quite different from Python. Any help will be beneficial. Thanks!

1 comment

0

do (1, #Back) and do that for the rest of them,this will generate it from 1 to Back

1 answer

✓ Accepted answer

math.random takes two numbers. The minimum value, and the maximum value. By default these are 0 and 1. When only one number is specified, it will presume that to be the maximum value.

API Documentation

In your script, you are attempting to get the number of children of "Front" and "Adjective" etc, however you are using it on the object at the moment, not the children table.

To do this, use: #Front:GetChildren()

This creates a table with all the child instances of "Front" which you can use, and '#' will return the length (or number of values inside) that table, and when put into the math.random function it will return a number between 1 and the number of children of "Front" (in this example).

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