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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 06 Apr 2016 11:32 AM |
| Ur... Name it 01Tree 02Bird? |
|
|
| Report Abuse |
|
|