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

How can I make a script that detects a player clicked a part through a lot of pieces in one folder?

Asked by 3 years ago

Hello guys.

The title is kinda confusing because idk how to explain my trouble properly. I have a script that works with for _, child in pairs which detects all parts' child in one folder with a single script. So, instead of pasting this script to all parts in that folder separately;

function onClick()
game.Workspace.PlatesLeft.Value = game.Workspace.PlatesLeft.Value - 1
script.Parent.Transparency = 0
script.Parent.CanCollide = true
end
script.Parent.ClickDetector.MouseClick:Connect(onClick)

I want it to do the same thing with only one script instead of pasting that script inside of the parts in that folder.

function onClick()
        for _, child in pairs(script.Parent: ... ) do -- "..." is where it gets confusing for me.
        if child.ClassName == "Part" then
            game.Workspace.PlatesLeft.Value=game.Workspace.PlatesLeft.Value - 1
            child.Transparency = 0
            child.CanCollide = true
        end
        child.ClickDetector.MouseClick:Connect(onClick)
    end
end

The problem is, I have no idea what to put instead of "..." I've tried :GetChildren() and didn't work. What should I do now?

NOTE: I DON'T want something like: when a part is clicked once all part's transparency in that folder will set to "0". I want that only one part's transparency will set to "0" which is clicked by a player. I hope you understand!

0
Instead of pasting all the scripts and all, have you ever thought that you can approach the problem by getting all of the parts, with using GetChildren(), and then make a function so that when you run the code, the function will work for all the parts? Xyternal 247 — 3y

Answer this question