Hello, this is my first post on these forums, so I apologize if this isn’t formatted correctly, but here’s the gist of it: I’m newer to scripting and I was experimenting w/ some coding to change the color of a brick, but I’m having some issues trying to convert the code to a function instead of a variable. I’ve already checked the Color3 and BrickColor pages in the API reference already, but I don’t seem to understand what I need to do to get it to work as a function instead of a variable.
This is the code I had when I was using a variable and it works just fine:
local PartSeeThroughValue = .3 +.2
local Part1 = game.Workspace.Part1
local ColorChange = BrickColor.new("New Yeller")
Part1.Material = "Concrete"
Part1.BrickColor = ColorChange
Part1.Transparency = PartSeeThroughValue
And this is the code I have atm when using a function:
local PartSeeThroughValue = .3 +.2
local Part1 = game.Workspace.Part1
local function ColorChange()
BrickColor.palette(1009)
end
Part1.Material = "Concrete"
Part1.BrickColor = ColorChange()
Part1.Transparency = PartSeeThroughValue
Your current code appears to take it granted that the function will be equal to what’s inside of it. This is not quite right, as running a function simply executes the code within it. They do not store values.
Given that you want your function call to become the value BrickColor.palette(1009), you can make use of the return statement. It will stop the execution of the function and make the place where it was called become the value that is returned. If you don’t include return within a functions, the call will be asigned the value nil.
Asuming you’re new to functions and lua in general, this guide on the developer hub might be useful to you. It even explains how return works!
I added the return and got an ‘Palette out of bounds’ error in the Output, which probably means that I used the wrong number I presume? I got the number from this link: BrickColor | Documentation - Roblox Creator Hub
Also, here is the code with the return added:
local PartSeeThroughValue = .3 +.2
local Part1 = game.Workspace.Part1
local function ColorChange()
BrickColor.palette(1009)
return BrickColor.palette(1009)
end
Part1.Material = "Concrete"
Part1.BrickColor = ColorChange()
Part1.Transparency = PartSeeThroughValue
While that issue is strange, I’d honestly recommend just doing BrickColor.new("New Yeller") than referencing certain numbers like this.
Alternatively, you could use the slightly more advanced Color3 constructor and use Color3.fromRGB(255, 255, 0). This allows you to control colors more finely. If you want to do this, just make sure to use Part1.Color instead of Part1.BrickColor when changing color values.
PS: Poster above points out a clear issue. Consider fixing it.
local PartSeeThroughValue = .3 +.2
local Part1 = game.Workspace.Part1
local function ColorChange(part,Colour)
part.BrickColor = BrickColor.new(Colour)
end
Part1.Material = "Concrete"
ColorChange(Part1,"New Yeller")
Part1.Transparency = PartSeeThroughValue