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

Adding functions to children of a model?

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

I have 30 blocks, and I want to add a Touched event individually to them so they can all run a function once they are ran over. How can this be done?

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Very simple: Just bind a touched event to each local brick inside the for loop.

for i=1,30, 1 do
    local Brick = Instance.new("Part")
    Brick.Name = "Brick" .. i
    local 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)
        --Add what you need to here.
    end)

end
1
I feel dumb, I can't believe I didn't think of this GarryGecko99 30 — 6y
0
Make sure that once you are done with the touched connection, you destroy the part its connected to, or disconnect it. masterblokz 58 — 6y
Ad

Answer this question