Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to detect if item in table got touched?

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.