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: Iterating through table, every value is nil

Previous Thread :: Next Thread 
TomsGames is online. TomsGames
Joined: 11 Oct 2013
Total Posts: 1615
31 Mar 2014 06:46 AM
creators = {
"carsroblox",
"carsme", -- I've defined this and carsothers elsewhere
"carsothers" --But for this it doesnt matter
}

_G["carsroblox"] = {"ModBlueCar",
"ModGreenJeep",
"ModPoliceCar",
"ModRedTruck",
"ModSchoolBus",
"ModSportsCar"}

function joe()
local creator = creators[1]
for i = 1, #(_G[creator]) do
print(_G[creator][i])
end
end

joe()




This script prints lots of nils. Why?
Report Abuse
TomsGames is online. TomsGames
Joined: 11 Oct 2013
Total Posts: 1615
31 Mar 2014 07:03 AM
Does nobody know ._.
Report Abuse
BenBonez is not online. BenBonez
Joined: 29 Aug 2008
Total Posts: 19362
31 Mar 2014 07:12 AM
I think this is more practical:

_G["Cars"] = {}

_G["CarsOfRoblox"] = {"ModBlueCar",
"ModGreenJeep",
"ModPoliceCar",
"ModRedTruck",
"ModSchoolBus",
"ModSportsCar"}

function joe()
for i, car in pairs(_G["Cars"]["CarsOfRoblox"]) do
print(car)
end
end

joe()
Report Abuse
TomsGames is online. TomsGames
Joined: 11 Oct 2013
Total Posts: 1615
31 Mar 2014 07:13 AM
Noo I don't think you understand, there are two variables. One picks a value from creators and that selects a table to read from, the second variable select a value from the second table.
Report Abuse
BenBonez is not online. BenBonez
Joined: 29 Aug 2008
Total Posts: 19362
31 Mar 2014 07:19 AM
That's essentially what mine does too without requiring extra variables and an extra table just for the indexes which is redundant. Here with an extra variable:

function joe()
local creator = _G["Cars"]["CarsOfRoblox"]
for i, car in pairs(creator) do
print(car)
end
end
Report Abuse
TomsGames is online. TomsGames
Joined: 11 Oct 2013
Total Posts: 1615
31 Mar 2014 07:21 AM
It works. Thanks :)
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