Change Model Material script

So I made a script that spawns parts into a model I created in the workspace.
it’s named ‘lag’.

and “rmv” is the model that I want to change the material.

And i set this script inside a UI Button, And I want this button to change the whole Model material.

here’s the script
script.Parent.MouseButton1Click:connect(function()

workspace.rmv.lag.Material = "Neon"

end)

it picks random single part that named as “lag” and changes it to only a single part, I want it to change the material to the whole model.

You can run a for loop on your model. It is unclear whether lag is a model or a part.

Use a for loop and loop through the part’s children. Then change each part’s material.

Something like this -

for i, part in pairs(workspace.rmv:GetChildren()) do
    part.Material = Enum.Material.Neon
end

I’d also use :IsA() to make sure the part is a BasePart in case you have other things in the rmv model.

2 Likes

(Sorry, reposting because I accidentally replied to the wrong person)

If lag is a model,

for _, part in ipairs(workspace.rmv.lag:GetDescendants()) do
    if part:IsA"BasePart" then
        part.Material = Enum.Material.Neon
    end
end

If you want to change every single part named “lag” within the rmv model,

for _, lag in ipairs(workspace.rmv:GetDescendants()) do
    if lag:IsA"BasePart" and lag.Name == "lag" then
        lag.Material = Enum.Material.Neon
    end
end
1 Like

Just wondering, are you using a local script or a server script? Because if you are using a localscript, the new material will only show to you.

Also, its better to check if the part name is lag before changing the material

script.Parent.MouseButton1Click:connect(function()
    for i, part in pairs(game:GetService("Workspace"):WaitForChild("rmv"):GetChildren())
        if part.Name == "lag" and part:IsA("Part") then
            part.Material = Enum.Material.Neon
        end
    end
end)
1 Like
for i,v in pairs (rmv:GetChildren()) do --get all children from model
if v:IsA("BasePart") and v.Name == "lag" then --check childrens name and classname
v.Material = "Neon"
end
end
1 Like