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).
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?
local numbers = {1,2,3,4,5,6,7,8,9,10} -- whatever
local function randomNumber()
return numbers[Random.new():NextInteger(1, #numbers)])
end
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 Random.new():NextInteger(min, 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.
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.
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
wait(20)
end