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

Touched event only working for 1 out of 30 bricks?

	Tiles = Instance.new("Model")
	Tiles.Parent = game.Workspace
	Tiles.Name = "Tiles"
	Tiles.Archivable = true
	brickArray = {}
	red = 30
	
for i=1,30, 1 do
	Brick = Instance.new("Part")
	Brick.Name = "Brick" .. i
	Name = Brick.Name
	table.insert(brickArray, Brick.Name)
	Brick.Parent = game.workspace.Tiles
	Brick.Anchored = true
	Brick.Size = Brick.Size + Vector3.new(20,0,22)
	Brick.Color = Color3.fromRGB(0,0,0)
	Brick.Locked = false
	if i < 6 then
		Brick.CFrame = CFrame.new(24*i,0,0)
		elseif i < 12 then
		Brick.CFrame = CFrame.new(24*(i-6),0,24)
		elseif i < 18 then
		Brick.CFrame = CFrame.new(24*(i-12),0,48)
		elseif i < 24 then
		Brick.CFrame = CFrame.new(24*(i-18),0,72)
		elseif i < 30 then
		Brick.CFrame = CFrame.new(24*(i-24),0,96)
	end
	
		Brick.Touched:Connect(function(hit)
			Brick.Color = Color3.fromRGB(10, 0, 255)
		end)
end

This code only works for the last brick created, not the other 29. The other 29 bricks actually do have touched events, as in the game sense I touched them, but the color is applied to the 30th brick instead of the brick that was actually touched.

My other issue is that it's detecting the touching too much. I just want the touched event to trigger once every time a player walks on the brick. They should have to walk off and then walk back on for the event to be triggered again, but the forums don't have any information on how to make the event only apply the first time.

1 answer

You have to get all of the Bricks after the game creates the bricks. The easiest way I know is to add a Debounce to your script.

local db = true
for i,v in pairs(workspace.Tiles:GetChildren()) do
	v.Touched:Connect(function()
		if db then
			db = false
			v.BrickColor = Color3.fromRGB(10, 0, 255)
			wait()
			db = true
		end
	end)
end

1 comment

0

I agree

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