tidus019
|
  |
| Joined: 12 Nov 2008 |
| Total Posts: 132 |
|
|
| 19 May 2013 10:45 AM |
Hello, here's what I would like to do.
_G["GiveColour"] = function(name,colour) local found = game.Workspace.namehere:FindFirstChild("TPart", true) if found then found.Name = "CPart" found.BrickColor = colour end end
I want to replace "namehere" in "game.Workspace.namehere" with the argument "name" passed by calling the global "GiveColour" function. But I don't know and can't seem to find what the format for variables in an Instance Hierarchy path has to be.
(Note: It's probably not important that it's a global function.)
Thanks. |
|
|
| Report Abuse |
|
|
|
| 19 May 2013 10:49 AM |
This won't work if there is more than one thing in workspace with the Name name.
_G["GiveColour"] = function(name,colour) local namething = game.Workspace:FindFirstChild(name) if namething then local found = namething:FindFirstChild("TPart", true) if found then found.Name = "CPart" found.BrickColor = colour end end end |
|
|
| Report Abuse |
|
|
|
| 19 May 2013 10:57 AM |
_G["GiveColour"] = function(name,colour) local found = game.Workspace[name]:FindFirstChild("TPart", true) if found then found.Name = "CPart" found.BrickColor = colour end end
Gamer's method is slower than mine. |
|
|
| Report Abuse |
|
|
tidus019
|
  |
| Joined: 12 Nov 2008 |
| Total Posts: 132 |
|
|
| 19 May 2013 11:02 AM |
| Aha! Thanks! It's very obvious now you told me. |
|
|
| Report Abuse |
|
|
tidus019
|
  |
| Joined: 12 Nov 2008 |
| Total Posts: 132 |
|
|
| 19 May 2013 11:06 AM |
| @ArceusInator Aha, so that's the format for using a variable in a Hierarchy. Good to know, since it seems more obvious than using 2 FindFirstChild methods. |
|
|
| Report Abuse |
|
|
|
| 19 May 2013 11:13 AM |
| Yep. Using that, you can have any variable as an index. Objects, other tables, events, functions, anything. |
|
|
| Report Abuse |
|
|
tidus019
|
  |
| Joined: 12 Nov 2008 |
| Total Posts: 132 |
|
|
| 19 May 2013 11:19 AM |
I see. Now I have this function.
_G["GiveColour"] = function(name,colour) local found = game.Workspace[name]:FindFirstChild("TPart", true) if found then found.Name = "CPart" found.BrickColor = BrickColor.new(colour) end end
Upon calling _G.GiveColour("tester","Red"), the following happens: One "TPart" changes name and colour (Dark Stone Grey, but I'll figure that one out later I guess)
I remember seeing "for i,v in pairs(t) do..." in scripts and the wiki before. But how do I make it so that it adds every child called "TPart" to a list called t? |
|
|
| Report Abuse |
|
|
tidus019
|
  |
| Joined: 12 Nov 2008 |
| Total Posts: 132 |
|
|
| 19 May 2013 11:27 AM |
| I could use GetChildren() somehow, but it seems inefficient. |
|
|
| Report Abuse |
|
|
|
| 19 May 2013 11:31 AM |
GetChildren would be the best way.
for _,child in pairs(thing:GetChildren()) do if child.Name == "TPart" then table.insert(whatevertable, child) end end |
|
|
| Report Abuse |
|
|
tidus019
|
  |
| Joined: 12 Nov 2008 |
| Total Posts: 132 |
|
|
| 19 May 2013 11:38 AM |
Ah, GetChildren returns a read-only table of an object's children, so it can be used in pairs! Interesting. I just realised I can just re-colour it and don't have to insert it into another table.
But if I did, would the table be local to the event? As in: If I call GiveColour 2 times, will the table "whatevertable" contain the results of GetChildren from the previous time(s) I called it? |
|
|
| Report Abuse |
|
|
|
| 19 May 2013 11:47 AM |
| It would depend on where you define the table. If it's local to the scope of GiveColour, then it would only contain things from that run. Otherwise it would contain everything. |
|
|
| Report Abuse |
|
|
tidus019
|
  |
| Joined: 12 Nov 2008 |
| Total Posts: 132 |
|
| |
|