hunte922
|
  |
| Joined: 28 May 2008 |
| Total Posts: 6969 |
|
|
| 21 Nov 2012 05:04 PM |
OK, here's my dilemma.
I need a script (ONLY ONE SCRIPT) that gathers all parts within a model and sets OnTouched functions to them.
And the reward?
The first person to post working code, if in Builder's Club, will receive 1kR$.
Notes: - Only one script is allowed, inserting scripts into the parts within the model is not what I am asking for. - Posting a link to a model that has what I am asking for, not made by the poster, will receive, if in Builder's Club, 500R$ instead of 1kR$. - Payment will be done via t-shirt sales. |
|
|
| Report Abuse |
|
|
nrscsy
|
  |
| Joined: 09 Nov 2012 |
| Total Posts: 465 |
|
|
| 21 Nov 2012 05:09 PM |
FunctionFunction() --your script end
for _,v in pairs(workspace["Model"]:getChildren()) do if v:IsA("BasePart") then v.Touched:connect(FunctionFunction) end end |
|
|
| Report Abuse |
|
|
|
| 21 Nov 2012 05:12 PM |
I don't request payment as this is ridiculously easy, ALSO DO NOT POST REQUESTS, i will give you a script this one time, but in future go to the "Let's make a deal forum" if you're planning on trading.
while wait() do for index, child in pairs(script.Parent:GetChildren()) do
child.Touched:connect(function(Part) print(child.Name.. "hit") -- if any part in the model is touched, this will print end)
end end
this should work alright, although i'm sure there's more efficient ways of doing it |
|
|
| Report Abuse |
|
|
|
| 21 Nov 2012 05:14 PM |
| Yeah, nrscsy's script is better :P |
|
|
| Report Abuse |
|
|
L2000
|
  |
| Joined: 03 Apr 2008 |
| Total Posts: 77448 |
|
|
| 21 Nov 2012 06:16 PM |
And if the parts are inside a main model, but then deeper (For example, located within other models), use this:
function onTouched(hit) -- Your code end
function getAllPartsWithin(parent) for i,v in pairs(parent:GetChildren()) do
if (v:IsA("Part")) then v.Touched:connect(onTouched); end
if (#v:GetChildren() > 0) then -- Run this on parts and others by ending first if getAllPartsWithin(v); end end end
getAllPartsWithin(workspace);
That's a simple code; using recursion (Running a function over and over, inside the function) it searches everything inside the parent for parts, then every part it finds gets the Touched event. Using that, you can have as many models as you want, and it will still find the parts (Unless it starts using up too much memory.. Don't worry about that, though). |
|
|
| Report Abuse |
|
|