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

How can I make it so that when I touch a part from a folder of parts the same function happens?

Asked by 2 years ago
Edited 2 years ago

So, I have tried something that i thought could work, turns out it really doens't

Basically i have 3 parts in the workspace and i put them all inside a folder named "Parts"

After that I create a table named parts which has all of the Folder's parts in it (hence why i used game.Workspace.Parts:GetChildren())

Then i ran a for loop so when a player (well, right now when anything) touches any of the parts, a print function runs

I thought this would work, but it really doesn't, can anyone help? I'd appreciate it

local parts = {game.Workspace.Parts:GetChildren()}

for i, v in pairs(parts) do
    v.Touched:Connect(function()
        print("one of the parts has been touched")
    end)
end

1 answer

Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

Your parts variable looks like this visually:

{
    {
        [1] = Part,
        [2] = Part,
        [3] = Part
    }
}

See the problem? A table inside a table, so the only thing you get when looping through the parts variable is only:

{
    [1] = Part,
    [2] = Part,
    [3] = Part
}

To solve the problem you would need to remove the curly brackets {} around the game.Workspace.Parts:GetChildren().

Fixed code:

local parts = game.Workspace.Parts:GetChildren()

for i, v in pairs(parts) do
    v.Touched:Connect(function()
        print("one of the parts has been touched")
    end)
end
0
thank you very much, i appreciate the help lolmarios2647 46 — 2y
Ad

Answer this question