How can I make one script's job apply to all the parts in a model instead of having to paste the script in each part seperatly?

Ok so I’m trying to make stairs that change colors when you step on them, and as I said in the title I want to try and make one script apply to all the parts in a model instead of having to take the time to paste that one tiny script into all the parts of the model. Here is the script:

script.Parent.Touched:Connect(function()
    script.Parent.BrickColor = BrickColor.random()
end

I consulted someone and they told me to “loop the script to insert it into each part automatically”, but I’m not quite sure how to do that. Any advice?

2 Likes
for i,v in pairs(Model:GetChildren()) do
    v.Touched:Connect(function(Hit)

    end)
end

Edit: If you have children inside the model that isn’t a BasePart then simply add an if statement to check. if v:IsA("BasePart") then

To expand on that, heres some notes using that script.

for i,v in pairs(Model:GetChildren()) do -- iterates through every part
    v.Touched:Connect(function(Hit) -- hit is the part, so you would do,
    if v:IsA("BasePart") then -- checks if its a part
        v.BrickColor = BrickColor.random() -- sets the color
    end)
end
1 Like

If each part in the model is named “Step” would I change (“BasePart”) to (“Step”)?

No, Change it to. if v.Name == "Step" then

No, the :IsA() checks what type of object it is. Not the name, if you wanted to check the name, you could do

if v["Name"] == "Step" then

you can also combine them,

if v:IsA("BasePart") and v["Name"] == "Step" then

By doing, GetChildren() this will form a list of parts that is recognised by the script as Parts.
the loop will loop through all of the items in the list. i is the number where the item is on the list. v is the actual item. Set Parts to the model with the parts in it. In the space between the loop and end, you put whatever you want the part to do.

local Parts = game.Workspace:GetChildren()

for i, v in pairs(Parts) do
	--whatever you want the part to do here
	print(v)
end
1 Like

so, and this is probably shot to sunshine, is this right?

for i,v in pairs(Model:GetChildren()) do -- iterates through every part
    v.Touched:Connect(function(Hit) -- hit is the part, so you would do,
    if v:IsA("BasePart") and v["Step"] == "Step" then
        hit.BrickColor = BrickColor.random() -- sets the color
    end)
end

Change the v["Step"] to v["Name"] and you’re good to go!

Do not do ["Property"] for defining properties. It is a bad practise and should only be used when creating systems that edit properties based on strings provided.

1 Like

So…ye?

for i,v in pairs(Model:GetChildren()) do -- iterates through every part
    v.Touched:Connect(function(Hit) -- hit is the part, so you would do,
    if v:IsA("BasePart") and v["Name"] == "Step" then
        hit.BrickColor = BrickColor.random() -- sets the color
    end)
end

It’s giving me this error

Workspace.Stairs.Script:6: Expected ‘)’ (to close ‘(’ at line 2), got

You forgot an end. Fixed Code:

for i,v in pairs(Model:GetChildren()) do -- iterates through every part
    v.Touched:Connect(function(Hit) -- hit is the part, so you would do,
    if v:IsA("BasePart") and v["Name"] == "Step" then
        hit.BrickColor = BrickColor.random() -- sets the color
        end
    end)
end

Odd, it still refuses to change the color of the brick when I step on the parts

The new error is - Workspace.Model.Script:1: attempt to index nil with ‘GetChildren’

Im going to assume the stairs are grouped in a model, if you haven’t already put the Script in the same model and add above the for i,v in pairs this variable. local Model = script.Parent

local Model = Script.Parent
for i,v in pairs(Model:GetChildren()) do -- iterates through every part
   v.Touched:Connect(function(Hit) -- hit is the part, so you would do,
   if v:IsA("BasePart") and v["Name"] == "Step" then
       hit.BrickColor = BrickColor.random() -- sets the color
       end
   end)
end

Workspace.Model.Script:1: attempt to index nil with ‘Parent’

Maybe I set it up wrong

1 Like

Lowercase s for script. Don’t do Script.

whoooaaah, it just gave me a wave of errors after I lowecased the S

That’s a roblox error, toolbox and catalog has been having issues.