qrrrq
|
  |
| Joined: 27 Jan 2013 |
| Total Posts: 1252 |
|
|
| 03 Dec 2013 11:26 PM |
I don't understand how this isn't working. Output at the bottom.
Script:
numbers = {1,2,3,4,5,6} for _,v in ipairs(numbers) do print(v[math.random(1,#numbers)]) end
Output:
23:23:54.257 - Workspace.Script:96: attempt to index local 'v' (a number value) 23:23:54.258 - Script 'Workspace.Script', Line 96 23:23:54.258 - stack end |
|
|
| Report Abuse |
|
|
Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
|
| 03 Dec 2013 11:28 PM |
| What are you trying to do? |
|
|
| Report Abuse |
|
|
qrrrq
|
  |
| Joined: 27 Jan 2013 |
| Total Posts: 1252 |
|
|
| 03 Dec 2013 11:30 PM |
| Print a random number from the table, sorry, forgot to put that. |
|
|
| Report Abuse |
|
|
|
| 03 Dec 2013 11:34 PM |
| print(numbers[math.random(1,#numbers)]) |
|
|
| Report Abuse |
|
|
qrrrq
|
  |
| Joined: 27 Jan 2013 |
| Total Posts: 1252 |
|
|
| 03 Dec 2013 11:38 PM |
| I know that works, but shouldn't I be able to use the 'v' variable? If not then why is it there? |
|
|
| Report Abuse |
|
|
|
| 03 Dec 2013 11:40 PM |
Oh i see.
for _,v in pairs(numbers) do print(v) end
|
|
|
| Report Abuse |
|
|
qrrrq
|
  |
| Joined: 27 Jan 2013 |
| Total Posts: 1252 |
|
|
| 03 Dec 2013 11:45 PM |
| Yes, I know, but why doesn't that work with a random number? |
|
|
| Report Abuse |
|
|
|
| 03 Dec 2013 11:50 PM |
v is the number
try
numbers = {1,2,3,4,5,6} for _,v in ipairs(numbers) do print(numbers[math.random(1,#numbers)]) end |
|
|
| Report Abuse |
|
|
qrrrq
|
  |
| Joined: 27 Jan 2013 |
| Total Posts: 1252 |
|
| |
|
oxcool1
|
  |
| Joined: 05 Nov 2009 |
| Total Posts: 15444 |
|
|
| 04 Dec 2013 01:04 AM |
| You're trying to print random value everytime? |
|
|
| Report Abuse |
|
|
qrrrq
|
  |
| Joined: 27 Jan 2013 |
| Total Posts: 1252 |
|
|
| 04 Dec 2013 01:07 AM |
| I am trying to print the entire table of numbers in a random order, but I thought I could use the 'v' variable, but ROBLOX won't let me for some reason. |
|
|
| Report Abuse |
|
|
|
| 04 Dec 2013 01:13 AM |
| Let me see if I can help explain, and help create a solution as well. Bear with me I will take a bit to write it out. |
|
|
| Report Abuse |
|
|
qrrrq
|
  |
| Joined: 27 Jan 2013 |
| Total Posts: 1252 |
|
|
| 04 Dec 2013 01:14 AM |
| Alright, if it isn't possible, then I'll just use the variable-less one, haha. |
|
|
| Report Abuse |
|
|
|
| 04 Dec 2013 01:24 AM |
The problem with: numbers = {1,2,3,4,5,6} for _,v in ipairs(numbers) do print(v[math.random(1,#numbers)]) end
Is that 'v' is an integer, not a table. 'v' represents the variable inside the table that ipairs is referencing. So for example
letters = {"a","b","c"} --Initialize a table for i,v in ipairs(letters ) do --Start a loop that will iterate through "letters" print("Key:"..i.." Value:"..v) --Print things to the output so we can see them end Will result in: Key:1 Value:a Key:2 Value:b Key:3 Value:c
If you want to fetch a SINGLE random number from the table "numbers" then you need to reference the table, at a random key: print(numbers[math.random(1,#numbers)])
You can do that as many times as you want no problem. Shuffling is a bit harder. Actually having an array returned shuffled requires a bit more scripting knowledge, but still utilizes basic principles. I'll explain using my previous example:
Since we already defined "letters" and saw it printed out in order as we defined it, we know it is setup properly. Next we just write a line to shuffle.
for i,v in ipairs(letters) do -- Start a loop that will iterate through "letters" local r = math.random(1,#letters) -- Select a random key in the table letters[i], letters[r] = letters[r], letters[i] --Swap the current key with the random key end
Tada, the table has been shuffled! If we print it again using the code we wrote to do so, you will see that it will go through all the keys in order again (hitting each once) but this time the values will be in a random order!
|
|
|
| Report Abuse |
|
|