generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: How do you make "Touched" work with multiple parts?

Previous Thread :: Next Thread 
jaghalkade is not online. jaghalkade
Joined: 29 Mar 2015
Total Posts: 96
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
Informable is not online. Informable
Joined: 10 Aug 2016
Total Posts: 1778
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
jaghalkade is not online. jaghalkade
Joined: 29 Mar 2015
Total Posts: 96
22 Dec 2016 06:05 AM
i'll try it


Report Abuse
chris2010_cy is online. chris2010_cy
Joined: 04 Jun 2016
Total Posts: 5
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 is not online. 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
Horrible_Pun is not online. Horrible_Pun
Joined: 13 May 2017
Total Posts: 5102
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
Spectrum555 is not online. Spectrum555
Joined: 20 Jul 2010
Total Posts: 171
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
Informable is not online. Informable
Joined: 10 Aug 2016
Total Posts: 1778
04 Sep 2017 01:44 AM
are you guys blind because this thread is from 9 months ago


Report Abuse
vastqud is not online. vastqud
Joined: 10 Sep 2011
Total Posts: 2464
04 Sep 2017 01:46 AM
yo whAT I DIDNT EVEN NOTICE THAT


Report Abuse
KingLoneCat is not online. KingLoneCat
Joined: 02 May 2014
Total Posts: 29
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image