Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-2

How can I detect whether any of the children are touched?

Asked by
Zerio920 285 Moderation Voter
10 years ago

Say I have a group of models, and I want some script to happen when any of the children of the model are touched. I think can do

    for i,v in ipairs(children) do
        v.Touched:connect(function(hit)
            --script
        end)

But I wouldn't want the script to repeat if say, I touch two of the children at the same time. Any ideas?

0
I would re-word the title... kieranm9090 49 — 10y

2 answers

Log in to vote
1
Answered by 10 years ago

This can be accomplished by using a function, and a Debounce Identifier, also, there is a few problems with your code: You are not checking if identifier v is a BasePart, and you forgot an end to the code, for the for loop. Let me explain;

Workspace
    Model
        Script
        Part
        Part
        Model

And if you called your script;

Workspace
    Model
        Script --Attempt to call event 'Touched' on a 'Script' type instance
        Part --Valid
        Part --Valid
        Model --Attempt to call event 'Touched' on a 'Model' type instance

If you leave it as it is, your code will error, and then break, we can fix this by using the if then end statement, also, your code is calling an anonymous [unknown] function, thus may cause an error in the future, let's fix up your code now;

local Debounce = false --Here is our Debounce; This will help to prevent the event from firing the function multiple times at once

function onTouch(hit) --Here is our new function, onTouch, this will be used later
if hit and hit.Parent and not Debounce then --The 'if then end' statement is checking is 'hit', 'hit.Parent', are existant, and identifier 'Debounce' is false
Debounce = true --Changes identifier 'Debounce' from false to true, this will prevent the function from being fired multiple times at once, as mentioned earlier
--Whatever your code is
wait(2) --This will wait '2' seconds before going on to the next block of code
Debounce = false --Changes identifier 'Debounce' from true, back to false, allowing the user to beable to use the 'onTouch' function again
end --This ends the code block for the 'if then end' statement
end --This ends the code block for the function

for i,v in pairs(script.Parent:GetChildren()) do --I am assuming this is in a 'Model' type instance, or is in the 'Workspace'; This will loop through all the children within the Script's Parent, creating a table
if v:IsA("BasePart") then --This will check to see if the current Child being looped through is a 'BasePart' type instance [WedgePart, Part, Truss, ect.]
v.Touched:connect(onTouch) --This will now connect the function 'onTouch' to the part; Whenever the 'BasePart' type instance is stepped on, it will fire the 'onTouch' function. Surprized you can do this in a code like this? :P
end --This ends the code block for the 'if then end' statement
end --This ends the code block for the 'for' loop

And this also answers your question to how to keep from firing when both parts are stepped on, this uses the same Debounce for each part, and if either one are stepped on, the function will revert it to false before another one fires, prevent from firing when two parts are stepped on at the same time. Hope this helped!

Ad
Log in to vote
0
Answered by 10 years ago

If you want to see if 2 children are touched at the SAME time, you could:

script.Parent.PartOne.Touched:connect(function(player) --1 part touched
Part1Touched = true --We need this for later
--Maybe some more codes
wait()
Part1Touched = false --Debounce!
end)
script.Parent.PartTwo.Touched:connect(function(player)
Part2Touched = true --Again... For later
--More codes maybe
wait()
Part2Touched = false--Another Debounce!!
end)
if Part1Touched and Part2Touched then
--BOTH TOUCHED!!! Now code it!
else
--Either 1 is touched, or none
end

That's one alternate. Your method should work too though

0
That could work... seems like it would get messy if I had more than 2 children though... Zerio920 285 — 10y

Answer this question