|
| 22 Dec 2016 06:00 AM |
I have a group of parts in a model, how would i make it so if any of those parts are touched it would fire a function in a script?
|
|
|
| Report Abuse |
|
|
|
| 22 Dec 2016 06:03 AM |
you can iterate through your model to find all parts using recursion, then connect them to your function
#code function touchedHandler(hit) -- touched stuff end function connectTouched(obj) for _,v in next,obj:children() do if v:IsA("BasePart") then v.Touched:connect(touchedHandler) end connectTouched(v) end end connectTouched(game.Workspace.YourModel)
|
|
|
| Report Abuse |
|
|
| |
|
|
| 03 Sep 2017 01:40 PM |
| I don't think this works. Tried it wit my own implementation and I get no events firing. |
|
|
| Report Abuse |
|
|
vastqud
|
  |
| Joined: 10 Sep 2011 |
| Total Posts: 2464 |
|
|
| 03 Sep 2017 01:49 PM |
i would do something like this:
local touchables = {workspace.part1, workspace.part2}
for i,v in pairs(touchables) do v.Touched:Connect(function(hit) if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then print(v.Name) end end end
|
|
|
| Report Abuse |
|
|
|
| 03 Sep 2017 01:50 PM |
"#code function touchedHandler(hit) -- touched stuff end function connectTouched(obj) for _,v in next,obj:children() do if v:IsA("BasePart") then v.Touched:connect(touchedHandler) end connectTouched(v) end end connectTouched(game.Workspace.YourModel)"
this is a little bit unorganized but theres a chance it could work |
|
|
| Report Abuse |
|
|
|
| 03 Sep 2017 02:30 PM |
function ontouched(hit) --do stuff end
model = workspace.Model:GetChildren() -- or wherever your model is lol
for i = 1, #model do model[i].Touched:connect(ontouched) end
|
|
|
| Report Abuse |
|
|
|
| 04 Sep 2017 01:44 AM |
are you guys blind because this thread is from 9 months ago
|
|
|
| Report Abuse |
|
|
vastqud
|
  |
| Joined: 10 Sep 2011 |
| Total Posts: 2464 |
|
|
| 04 Sep 2017 01:46 AM |
yo whAT I DIDNT EVEN NOTICE THAT
|
|
|
| Report Abuse |
|
|
|
| 04 Sep 2017 01:52 AM |
I would simply do the following:
local model = workspace.Parts -- All of the parts that you need the touched event to effect.
local function touch(hit) -- Touched stuff end
local function touchify_model(mod) for _, part in next, mod:GetChildren() do if part:IsA("Part") then part.Touched:Connect(touch) end end end
touchify_model(model)
~ KingLoneCat |
|
|
| Report Abuse |
|
|