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

How to detect if item in table got touched?

Asked by
MAKKU 37
5 years ago

I want to make it so when one of the bricks inside the table gets touched it prints something. Here's what i got so far:

bricks = {}

for i,v in pairs(game.Workspace.Bricks:GetChildren()) do

    table.insert(bricks,v)

end

unpack(bricks).Touched:Connect(function()
    print("touched")
end)

1 answer

Log in to vote
3
Answered by
0_2k 496 Moderation Voter
5 years ago

Here's an example of what you're looking for

bricks = {}

for _,v in pairs(workspace.Bricks:GetChildren()) do
    table.insert(bricks, v)
    --[[v.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") then --Delete if you don't want to check here
            print'touched'
        end
    end)]]
end

for _,v in pairs(bricks) do -- Iterates through the "bricks" table
    v.Touched:Connect(function(touched) -- checks if anything within the table is touched
        if touched.Parent:FindFirstChild("Humanoid") then -- checks if it has a humanoid
            print'hit' -- prints hit if it does
        end
    end)
end
Ad

Answer this question