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: Sorting a table of functions

Previous Thread :: Next Thread 
dennis96411 is not online. dennis96411
Joined: 06 Mar 2009
Total Posts: 1079
11 Sep 2013 04:35 PM
I've put most of my functions into a table, but the thing is when I use a for loop to loop through the table to load each function, they don't load in the order they were declared. Example:
=========================
a = {
b = function()
print("Herp")
end,
c = function()
print("Derp")
end
}

for _,function in pairs(a) do
ypcall(function)
end
=========================
It loads function c first, then it loads function b. Is there a way to have the table sorted the way it was declared?
Report Abuse
Xeptix is not online. Xeptix
Joined: 14 Mar 2013
Total Posts: 1115
11 Sep 2013 04:36 PM
for _,function in ipairs(a) do
ypcall(function)
end
Report Abuse
dennis96411 is not online. dennis96411
Joined: 06 Mar 2009
Total Posts: 1079
11 Sep 2013 04:39 PM
I changed "pairs" to "ipairs" and it ended up not working at all. By the way, I changed "function" to "func" since I can't use that variable.
Report Abuse
Apatheia is not online. Apatheia
Joined: 16 Aug 2013
Total Posts: 198
11 Sep 2013 04:41 PM
You can't unless you use numerical indexes and ipairs.
Report Abuse
Absurdism is not online. Absurdism
Joined: 18 Jul 2013
Total Posts: 2568
11 Sep 2013 04:41 PM
Don't use 'function' as a variable name.

a = {
b = function()
print("Herp")
end,
c = function()
print("Derp")
end
}

for _,func in pairs(a) do
ypcall(func)
end

See what that does. Otherwise:

a = {
b = function()
print("Herp")
end,
c = function()
print("Derp")
end
}

table.sort(a)
for _,func in pairs(a) do
ypcall(func)
end
Report Abuse
dennis96411 is not online. dennis96411
Joined: 06 Mar 2009
Total Posts: 1079
11 Sep 2013 04:45 PM
table.sort did nothing.
Report Abuse
dennis96411 is not online. dennis96411
Joined: 06 Mar 2009
Total Posts: 1079
11 Sep 2013 05:16 PM
Bump.
Report Abuse
dennis96411 is not online. dennis96411
Joined: 06 Mar 2009
Total Posts: 1079
11 Sep 2013 05:50 PM
More bumping.
Report Abuse
Apatheia is not online. Apatheia
Joined: 16 Aug 2013
Total Posts: 198
11 Sep 2013 08:54 PM
a = {
function()
print("Herp")
end,
function()
print("Derp")
end
}

for _, v in ipairs(a) do
ypcall(v)
end
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
11 Sep 2013 10:12 PM
Since it's none-indexed, it's not going to be how it looks.

Just do:

a = {
{function() end}
{function() end}
}

for k_x in pairs(a) do
ypcall(x[1])
end
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