Search code examples
luaroblox

(Roblox scripting) help needed for "GetMaterialColor" or "SetMaterialColor"


I am new to roblox scripting, and I am working on a game. I am trying to make an exploration game with multiple planets. I want the colors on the surfaces of the planets to vary, but I also wish to use smooth terrain, as it is easier to use and looks nice. from reading a bit online, i have figured out i need to use "GetMaterialColor" or "SetMaterialColor". however, "SetMaterialColor", the one i needed specifically, requires two bits of information- the material and the color.

The issue comes from the "Material" part of this, as I have no idea how to make the script recognize what material I want to change. i tried multiple things, including but not limited to: (grass, #,#,#) (grass) (#,#,#) ("Grass"), (#,#,#) ("Grass", #,#,#) or even just (#,#,#), without trying to get a specific material at all

so yeah, I need some help

here is the code:

local function onTouch(hit)
    game.Workspace.Terrain:SetMaterialColor

end

script.Parent.Touched:connect(onTouch)

(there should be stuff after SetMaterialColor, that is what i need help with)


Solution

  • If you read the documentation on Terrain:SetMaterialColor(), you'll see that the first argument is a Material type, which is an Enum. So the method expects an Enum (or number to be more accurate), not a string denoting the material.

    At the same time the second argument is a Color3, so (#,#,#) isn't apt, using it with the constructor Color3.fromRGB(#,#,#) is. If you are ever confused about what a method returns or expects, try referring to its documentation on https://developer.roblox.com/.

    Here's an example of correct usage:

    workspace.Terrain:SetMaterialColor(Enum.Material.Grass, Color3.fromRGB(123,123,123))
    

    And ofcourse, Event:Connect() instead of Event:connect()