New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Insert a ClickDetector in all ClassName = "Part"?

Asked by
FiredDusk 1465 Moderation Voter
4 years ago
for i,v in pairs (game.Workspace:GetChildren()) do
    if v.ClassName == ("Part") then
        print 'hey' --Does print
        Instance.new("ClickDetector") --Right here, I am trying to insert a ClickDetector in all Parts
    end
end

1 answer

Log in to vote
2
Answered by 4 years ago

Looks like you did not specify the parent of the new ClickDetectors, look below!

for index, child in pairs(workspace:GetChildren()) do
    if child.ClassName == "Part" then
        print(child.Name .. " is a Part!") -- optional line
        local ClickDetector = Instance.new("ClickDetector")
        ClickDetector.Parent = child
    end
end

The code above would create ClickDetectors in each of the parts in Workspace

Ad

Answer this question