generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: For loops?

Previous Thread :: Next Thread 
qrrrq is not online. 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 is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
03 Dec 2013 11:28 PM
What are you trying to do?
Report Abuse
qrrrq is not online. 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
godphantom555 is not online. godphantom555
Joined: 12 Mar 2011
Total Posts: 360
03 Dec 2013 11:34 PM
print(numbers[math.random(1,#numbers)])
Report Abuse
qrrrq is not online. 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
godphantom555 is not online. godphantom555
Joined: 12 Mar 2011
Total Posts: 360
03 Dec 2013 11:40 PM
Oh i see.

for _,v in pairs(numbers) do
print(v)
end
Report Abuse
qrrrq is not online. 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
godphantom555 is not online. godphantom555
Joined: 12 Mar 2011
Total Posts: 360
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 is not online. qrrrq
Joined: 27 Jan 2013
Total Posts: 1252
03 Dec 2013 11:56 PM
Alright.
Report Abuse
oxcool1 is not online. 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 is not online. 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
TecmagDiams is not online. TecmagDiams
Joined: 18 Sep 2008
Total Posts: 1882
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 is not online. 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
TecmagDiams is not online. TecmagDiams
Joined: 18 Sep 2008
Total Posts: 1882
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image