wazap
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 23234 |
|
|
| 07 Dec 2013 01:34 PM |
I'm trying to figure out a way to sort out things in a model
array = {} for q, k in pairs(script.Parent.Model:children()) do if not array[k.Name] then array[k.Name] = 1 else array[k.Name] = array[k.Name] + 1 end end
Does this look right? Or are there any glaring errors? |
|
|
| Report Abuse |
|
|
|
| 07 Dec 2013 01:43 PM |
| I'm not exactly sure what your asking, can you possibly clarify the goal of this script. sort out things in a model is very vague, especially after looking at the script haha |
|
|
| Report Abuse |
|
|
wazap
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 23234 |
|
|
| 07 Dec 2013 01:50 PM |
Basically this counts out how many of an item with a certain name there is an a model. If there are 10 objects in a model, with 4 potatos, 2 apples, 3 noobs and 1 gun, I want it to be {"potatos" = 4, "noobs" = 3, "gun" = 1, "apples" = 2} |
|
|
| Report Abuse |
|
|
wazap
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 23234 |
|
| |
|
|
| 08 Dec 2013 02:12 AM |
List = { ["Carrot"] = 0, ["Potato"] = 0 } Items = {"Carrot","Carrot","Potato"}
for _,p in pairs(Items) do for i,v in pairs(List) do if i == p then List[i] = List[i] + 1 end end end
for i,v in pairs(List) do print(i .. ": " .. v) end |
|
|
| Report Abuse |
|
|
|
| 08 Dec 2013 02:13 AM |
| So, Erm, thanks to this I decided to stop being lazy and learned how dictionaries worked. |
|
|
| Report Abuse |
|
|
|
| 08 Dec 2013 02:17 AM |
Aren't they basically arrays of set values or something?
|
|
|
| Report Abuse |
|
|
wazap
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 23234 |
|
|
| 08 Dec 2013 02:37 AM |
Pretty much. But the syntax of it is weird :L
I'm mainly concerned with this line
if not array[k.Name] then array[k.Name] = 1
It seems weird. No red line, but somehow doesnt seem to run? |
|
|
| Report Abuse |
|
|
|
| 08 Dec 2013 02:38 AM |
if not array[k].Name then array[k].Name = 1 |
|
|
| Report Abuse |
|
|
|
| 08 Dec 2013 02:50 AM |
Wait... You were asking if yours worked? It should. |
|
|
| Report Abuse |
|
|
|
| 08 Dec 2013 03:22 AM |
Your script looks like it will work as intended. Let's look at an example:
Model > Part > Part > Chuck > Part > Norris > Chuck > Hammer
array = {} for q, k in pairs(script.Parent.Model:children()) do if not array[k.Name] then array[k.Name] = 1 else array[k.Name] = array[k.Name] + 1 end end
> Part not in array > array.Part = 1 > Part in array > array.Part = array.Part + 1 > Chuck not in array > array.Chuck = 1 > Part in array > array.Part = array.Part + 1 > Norris not in array > array.Norris = 1 > Chuck in array > array.Chuck = array.Chuck + 1 > Hammer not in array > array.Hammer = 1 |
|
|
| Report Abuse |
|
|