Value Randomiser Script

I am asking for help and support regarding a script that randomly changes the value of something. I have no idea how the script would go which is why I’m asking on here - the script I was thinking of would be a list of values and it would change the value in workspace to one of the values in the script every 10-20 seconds (in a random order of course).

This sounds quite complicated but that’s only due to the way I’m explaining it. In simple terms, how would you go about creating a code that changes a value to a different number chosen from a list in a script. (In a random order).

Any help would be greatly appreciated.

You could:

  • Have that list of numbers.
  • Get a random index from 1 to the amount of numbers in that list.
  • Get the element in the list that has that index local newNumber = list[randomNumber]
  • Change the value in the workspace to equal the newNumber
  • Put this all in a while loop that has a wait of 10-20 seconds
1 Like

I understand that I could do a while true do code and have it run a code that changes the value, what I don’t understand is how to actually script the code itself so that it chooses the random number out of my list. I am able to do the part that changes the value and loops it, along with the wait time. I just need help scripting the part that chooses a random number to set it as.

My reply after your post was for someone else, sorry - so how would I use this? By this, I mean what would this do, would it choose a number, and if so, how would I then change the value using this?

1 Like
 local numbers = {1,2,3,4,5,6,7,8,9,10} -- whatever

 local function randomNumber()
       return numbers[, #numbers)])

 print("random number was", randomNumber())

Call randomNumber() whenever you need a random number.

local number = randomNumber()
-- do anything with the number

It would select a value with the numerical index of x, where x is a random integer generated through, max).

No, only out of the numbers you defined, as it selects a number 1 through the amount of values you define in your numerically indexed table.

But wouldn’t this generate a completely random number instead of one out of my list?

For example, if I had a code like this:

local number =

while true do
game.Workspace.NumberGeneratorValue.Value = local number

Where would I use your code? (The local number would be the randomly generated number)

I apologize for all of this, I’ve never really done this type of code so I am very confused.

Say, you had a list of three numbers:
local list = {3,5,7}

You can then reference the values in that list like so:
list[1] would give you the value 3
list[2] would give you the value 5
list[3] would give you the value 7

So now that you know this, you can see that you need to get a random number (the index) that represents the first, second or third value in your list.

To get that random number, you can use local randomNumber = math.random(MIN, MAX)
where MIN would be 1 and MAX would be the amount of numbers in your list (#list).

list[randomNumber] would give you the first/second/third/etc number in the list.

1 Like

I explained how to get the random number above, and here is the code. I gotta go, so farewell and good luck.

local list = {1,2,4,65,7,9,555,54,34,55}

while true do
    local index = math.random(1, #list)
    local number = list[index]
    game.Workspace.NumberGeneratorValue.Value = number
1 Like