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

Changing materials through script?

Asked by 8 years ago

I tried to tell the script that when the brick was clicked, it would change from Plastic to Wood. It's properties show it's plastic. But when I test the script it doesn't work.

function changeMaterial()
    if script.Parent == "Plastic" then
        script.Parent = "Wood"
    end
end

script.Parent.ClickDetector.MouseClick:connect(changeMaterial)

1 answer

Log in to vote
1
Answered by 8 years ago

Okay, well, there's a few things wrong with this code.

function changeMaterial()
    if script.Parent == "Plastic" then --This tells the script to look for an object called "Plastic". This is wrong.
        script.Parent = "Wood" --Likewise, this is trying to change the parent of this, making it a child of "Wood". This throws an error because you supplied a string on an object request.
    end
end

script.Parent.ClickDetector.MouseClick:connect(changeMaterial)

You need to use an Enum.

function changeMaterial()
    if script.Parent.Material == Enum.Material.Plastic then
        script.Parent.Material = Enum.Material.Wood -- This changes the material, rather than the parent.
    end
end

script.Parent.ClickDetector.MouseClick:connect(changeMaterial)

You're welcome, tell me if you have any problems.

0
Thanks! EasilyTech 25 — 8y
Ad

Answer this question