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 » Scripters
Home Search
 

Re: Iterating in the order: 1Part, 2Tree 3House, etc. ?

Previous Thread :: Next Thread 
Craftero is not online. Craftero
Joined: 24 Jun 2011
Total Posts: 1451
06 Apr 2016 08:27 AM
If we have a Model and the name of each child of the Model begins with a number, like this:
1House,
2Bird,
3Tree,
etc.

How could we print in the order 1House, 2Bird, 3Tree, etc. without knowing what the child actually is?

For example, 1House could be 1Fence, or 1Dog, or anything else. The only thing we are certain of is that each child begins with a different number.

Does anyone know how this can be done?
Report Abuse
error9999 is not online. error9999
Joined: 05 Sep 2009
Total Posts: 1593
06 Apr 2016 08:33 AM
local tab = {}

for i , value in next, game.Workspace.SpecialModel:GetChildren() do
table.insert(tab,value)
end
table.sort(tab)

print(table.concat(tab, ", "))
Report Abuse
Craftero is not online. Craftero
Joined: 24 Jun 2011
Total Posts: 1451
06 Apr 2016 08:50 AM
@error9999

Thanks for the suggestion, but this gives the error:
"attempt to compare two userdata values"

It references line 6, table.sort(tab).

Any idea how this could be fixed?
Report Abuse
error9999 is not online. error9999
Joined: 05 Sep 2009
Total Posts: 1593
06 Apr 2016 08:57 AM
I suggest you to look at this:

http://wiki.roblox.com/index.php?title=Function_dump/Table_manipulation#table.sort

Maybe it does not work because the table now has Objects and not just their names, so use try this instead:

local tab = {}

for i , value in next, game.Workspace.SpecialModel:GetChildren() do
table.insert(tab,value.Name) -- so it now saves the name of the value
end
table.sort(tab)

print(table.concat(tab, ", "))
Report Abuse
Craftero is not online. Craftero
Joined: 24 Jun 2011
Total Posts: 1451
06 Apr 2016 09:07 AM
@error9999

Thanks for the help.

The only problem is that 10House and 11House, for example, are printed first rather than 1Tree being printed first.
Is there a simple way around this?
Report Abuse
error9999 is not online. error9999
Joined: 05 Sep 2009
Total Posts: 1593
06 Apr 2016 09:32 AM
Maybe if you get the number from the name, e.g 15 from "15Truck" or something, you can put that name on the 15th place in the table?

yet I have no idea on how this will turn out, because it'll basically move all values in the table.
Report Abuse
Craftero is not online. Craftero
Joined: 24 Jun 2011
Total Posts: 1451
06 Apr 2016 11:05 AM
@error9999

Yeah, I've tried that before with the following code:

Count = 1

for i, v in pairs (game.Workspace.SpecialModel:GetChildren()) do

if v.Name:sub(1,1) == tostring(Count) or v.Name:sub(1,2) == tostring(Count) then
Count = Count + 1
print(v)
end

end

..It prints 10House, 11Truck, 2Bird, 3Car, 4Man, etc.
I just can't figure out how to make it count without messing up on the double-digit names.

Would you know how I could edit this code to do it?
Report Abuse
Craftero is not online. Craftero
Joined: 24 Jun 2011
Total Posts: 1451
06 Apr 2016 11:07 AM
Correction.
That code should be:

ModelsFound = 0

repeat
for _, v in pairs(myModels:GetChildren()) do
if v.Name:sub(1,2) == 0 then
ModelsFound = ModelsFound + 1
print("culprit" .. v.Name)
end

if v.Name:sub(1,1) == tostring(Count) and v.Name:sub(2,2) ~= 0 then
Count = Count + 1
ModelsFound = ModelsFound + 1
print(v.Name)
wait(0.01)
end

end
wait(0.01)
until ModelsFound == (#myModels:GetChildren())
Report Abuse
Craftero is not online. Craftero
Joined: 24 Jun 2011
Total Posts: 1451
06 Apr 2016 11:07 AM
Above is my attempt at getting it to work properly.
It doesn't succeed.
Report Abuse
Craftero is not online. Craftero
Joined: 24 Jun 2011
Total Posts: 1451
06 Apr 2016 11:08 AM
Also, I should add "culprit" doesn't print, although I'm almost sure it should for 10House..?
Report Abuse
Craftero is not online. Craftero
Joined: 24 Jun 2011
Total Posts: 1451
06 Apr 2016 11:14 AM
Never mind.
I solved the problem.

Turns out tonumber() is more useful than you'd think!

Thank error9999 for your help.
Report Abuse
Protoduction is not online. Protoduction
Joined: 27 Jul 2012
Total Posts: 1054
06 Apr 2016 11:32 AM
Ur... Name it 01Tree 02Bird?
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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