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 loop

Previous Thread :: Next Thread 
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
08 Jun 2014 09:21 PM
for i,v in pairs(blah) do

for i,v in next, blah do

difference?
Report Abuse
cntkillme is not online. 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 is not online. 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
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
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 is not online. 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 is not online. 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
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
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 is not online. 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
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
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
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
08 Jun 2014 09:39 PM
so my recent post would be the most efficient?
Report Abuse
cntkillme is not online. 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 is not online. 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
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
08 Jun 2014 09:47 PM
i wanted it to yield until the number of maps is at least 1
Report Abuse
Bebee2 is not online. 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
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
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
FreeScriptMaker is not online. FreeScriptMaker
Joined: 29 Nov 2013
Total Posts: 2275
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
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
08 Jun 2014 09:52 PM
@beb what?
Report Abuse
cntkillme is not online. 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 is not online. 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
FreeScriptMaker is not online. FreeScriptMaker
Joined: 29 Nov 2013
Total Posts: 2275
08 Jun 2014 09:58 PM
ClassName is faster unless you need to check for subclasses.
Report Abuse
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
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 is not online. 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 is not online. 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
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
08 Jun 2014 10:03 PM
I have it down, but I just feel like posting.
Report Abuse
cntkillme is not online. 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
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