|
| 08 Jun 2014 09:21 PM |
for i,v in pairs(blah) do
for i,v in next, blah do
difference? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Jun 2014 09:23 PM |
| pairs returns next, but pairs, unlike next, is a function |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Jun 2014 09:23 PM |
| well next is a function, but not in that case. |
|
|
| Report Abuse |
|
|
|
| 08 Jun 2014 09:24 PM |
function choices() m = {} for i, v in pairs, game.ServerStorage:GetChildren() do if v:IsA("Model") then table.insert(m, v) end end end
I don't want to use parenthesis, so could that work? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Jun 2014 09:25 PM |
No, that wouldn't work Well technically it would work but it would not do what you are expecting |
|
|
| Report Abuse |
|
|
Bebee2
|
  |
| Joined: 17 May 2009 |
| Total Posts: 3985 |
|
|
| 08 Jun 2014 09:28 PM |
Best way to say it like Cnt: next is the actual table iterator. pairs is a function that returns next |
|
|
| Report Abuse |
|
|
|
| 08 Jun 2014 09:32 PM |
function choices() m = {} for i, obj in pairs(game.ServerStorage:GetChildren()) do if obj:IsA("Model") then table.insert(m, obj) end end return m end
So this is more efficient?
function choices() m = {} for i, obj in next, game.ServerStorage:GetChildren() do if obj:IsA("Model") then table.insert(m, obj) end end return m end |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Jun 2014 09:33 PM |
| next would, but either way you could be much more efficient by using a numerical loop. |
|
|
| Report Abuse |
|
|
|
| 08 Jun 2014 09:37 PM |
function select() m = {} for obj = 1, #game.ServerStorage:GetChildren() do if game.ServerStorage:GetChildren()[obj]:IsA("Model") then table.insert(m, game.ServerStorage:GetChildren()[obj]) end end if #m == 0 then select() end selected = m[math.random(#m)] return selected end
|
|
|
| Report Abuse |
|
|
|
| 08 Jun 2014 09:39 PM |
| so my recent post would be the most efficient? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Jun 2014 09:41 PM |
No, that's pretty bad because you're calling GetChildren more than needed, much more if there are a lot of stuff in ServerStorage.
local serverStorage = Game.ServerStorage; local children = serverStorage:GetChildren()
local function select() local m = {}; for key = 1, #children do local value = children[key]; if value:IsA("Model") then m[#m + 1] = value; end end return m[math.random(#m)]; end
Something like that would be better, if stuff is going to be added in serverStorage, you can either put it inside the loop or use a ChildAdded event to be more efficient |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Jun 2014 09:44 PM |
| and if #m is 0, why would you recall the function and get an overflow error instead of waiting until something was added? |
|
|
| Report Abuse |
|
|
|
| 08 Jun 2014 09:47 PM |
| i wanted it to yield until the number of maps is at least 1 |
|
|
| Report Abuse |
|
|
Bebee2
|
  |
| Joined: 17 May 2009 |
| Total Posts: 3985 |
|
|
| 08 Jun 2014 09:48 PM |
Not to be so picky but...
IsA is a function rather than ClassName which is an index =P... |
|
|
| Report Abuse |
|
|
|
| 08 Jun 2014 09:51 PM |
function select() m = {} tab = game.ServerStorage:GetChildren() for obj = 1, #tab do if tab[obj]:IsA("Model") then tab[obj] = m[#m + 1] end end local selected = m[math.random(#m)] return selected end
Please tell me this is ok |
|
|
| Report Abuse |
|
|
|
| 08 Jun 2014 09:51 PM |
local serverStorage = Game.ServerStorage; local children = serverStorage:GetChildren()
local function select() while #children == 0 do local child = children.ChildAdded:wait() if child:IsA("Model") then children = serverStorage:GetChildren() end end
local m = {}; for key = 1, #children do local value = children[key]; if value:IsA("Model") then m[#m + 1] = value; end end return m[math.random(#m)]; end |
|
|
| Report Abuse |
|
|
| |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Jun 2014 09:52 PM |
| You mean property, yes. But using IsA can check for super classes (BasePart, BaseScript, etc.) and might actually be faster since it's C-sided iirc |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Jun 2014 09:53 PM |
| FSM, why not just return break the loop after a new model is added? |
|
|
| Report Abuse |
|
|
|
| 08 Jun 2014 09:58 PM |
| ClassName is faster unless you need to check for subclasses. |
|
|
| Report Abuse |
|
|
|
| 08 Jun 2014 10:00 PM |
function select() m = {} tab = game.ServerStorage:GetChildren() for obj = 1, #tab do if tab[obj]:IsA("Model") then tab[obj] = m[#m + 1] end end if #m == 0 then return 1 % mod[m] end local selected = m[math.random(#m)] return selected end
? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Jun 2014 10:01 PM |
you mean supclasses? And don't say indexing ClassName is faster, IsA function is c-sided so IsA _might_ be faster. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Jun 2014 10:02 PM |
| Ignoring efficiency since you're ignoring me, jimmy, mod is nil |
|
|
| Report Abuse |
|
|
|
| 08 Jun 2014 10:03 PM |
| I have it down, but I just feel like posting. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 08 Jun 2014 10:05 PM |
| Do you plan on adding things in ServerStorage while the server is running? If not then why use GetChildren > 1? |
|
|
| Report Abuse |
|
|