Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Changing materials through script?

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

✓ Accepted answer

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.

1 comment

0

Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.