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

Touched event only working for 1 out of 30 bricks?

Asked by 5 years ago
    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

Log in to vote
0
Answered by
dirk2999 103
5 years ago
Edited 5 years ago

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
0
I agree Delude_d 112 — 5y
Ad

Answer this question