vals = { [1] = "1shfh", [2] = "2shfh", [3] = "3shfh", [4] = "4shfh", [5] = "5shfh", [6] = "6shfh", [7] = "7shfh", [8] = "8shfh", [9] = "9shfh", [10] = "10shfh" -- this is just a test table, if you were wondering } maps1 = math.random(1,#vals) table.remove(vals, maps1) maps2 = math.random(1,#vals) table.remove(vals, maps2) maps3 = math.random(1,#vals) table.remove(vals, maps3) maps4 = math.random(1,#vals) table.remove(vals, maps4) while wait() do print(maps1,maps2,maps3,maps4) end
it ALWAYS prints 9 9 2 3. its supposed to give me 4 different numbers, and i cant have them repeat, thats why there is the table.remove
If you're not getting different numbers everytime, try using math.randomseed
!
local vals = {1,2,3,4,5,6,7,8,9,10}; --Your table local maps = {}; for i = 1,4 do table.insert(maps,table.remove(vals,math.random(1,#vals))); end print(table.concat(maps,", "));
Overview:
You seem to be going about this correctly, though it could be a bit cleaner. But we'll get to that later.
Problem:
The problem is pretty simple. You're creating 4 individual variables
, each assigned a random value from your array (btw, assigning the numeric keys manually isn't necessary).
The problem
is, the values that were assigned to each of those variables you created, stay stored
in those variables. They won't change unless you manually do it, or have a function do it.
Solution:
We can fix this easily by putting our code inside the while loop, to refresh each operation. Here's a function i devised that grabs a random value from an array, implemented in your revised code:
local vals = { "first", "second", "third", "fourth", "sixth", "seventh", "eighth", "ninth", "tenth", } local function randomValue(t) return t[math.random(#t)] end -- Slow it down to make it a bit easier to see the changes while wait(1) do local randoms = {} -- Create the random variable 4 times within the "randoms" table for i = 1,4 do randoms["map"..i] = randomValue(vals) end -- Print all elements in the "randoms" table. print(unpack(randoms)) end
Hope it helped. Let me know if you have any questions.