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

How do I use FindFirstChild?

Asked by
Sam4550 42
9 years ago

When I use the script below, it only changes the MaxActivationDistance of one ClickDetector. I want it to do it to all the ClickDetectors it can find inside ModelA or at least on that level.

local a = game.Workspace.ModelA
local b = a:FindFirstChild("ClickDetector", true)

function onClicked()
    script.Parent.ClickDetector.MaxActivationDistance = 0
    wait(0.5)
    script.Parent.Parent.Base.Beep:Play()
    wait(1)
    script.Parent.Parent.Base.Beep:Stop()
    wait(0.5)
    if b then
        b.MaxActivationDistance = 0
    end
    wait(2)
    script.Parent.Parent.Off.ClickDetector.MaxActivationDistance = 32
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

I understand I'm not explaining it very well but I can't really explain it any better.

Thanks!

2 answers

Log in to vote
0
Answered by 9 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
function onClick()
    for key, index in pairs(Workspace.ModelA:GetChildren()) do
        if index:IsA('ClickDetector') then
        index.MaxActivationDistance = 0
        wait(.5)
        script.Parent.Parent.Base.Beep:Play()
        wait(1)
        script.Parent.Parent.Base.Beep:Stop()
        wait(0.5)
        index.MaxActivationDistance = 32
        end
    end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
Ad
Log in to vote
0
Answered by 9 years ago
local modelChildren = game.Workspace.ModelA:GetChildren() --the children of the model
local ClickDetectors = {} --a table to put the ClickDetectors in
local ready = true --a debounce, so it must finish before it starts again.

function onClicked()
    if ready then --if not in progress
        ready = false --make the script know it's in progress
        for i, v in ipairs(ClickDetectors) do --these three lines change the activation distance for all click detectors.
            script.Parent.ClickDetector.MaxActivationDistance = 0
        end
        wait(0.5)
        script.Parent.Parent.Base.Beep:Play()
        wait(1)
        script.Parent.Parent.Base.Beep:Stop()
        wait(0.5)
        for i, v in ipairs(ClickDetectors) do --same as before
            script.Parent.ClickDetector.MaxActivationDistance = 32
        end
        ready = true --finish the function
    end
end

for i, v in ipairs(a) do --this is what puts the click detectors in the table and connects the event.
    if v:IsA("ClickDetector") then
        script.Parent.ClickDetector.MouseClick:connect(onClicked)
        table.insert(ClickDetectors, v)
    end
end

Answer this question