Having trouble converting some code to a function instead of a variable

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

Any help would be appreciated, and thank you.

3 Likes

You have to return the brick color from the function.

3 Likes

It seems you are not returning the value you want to set it as.

local function ColorChange()
   return BrickColor.palette(1009)
end

You can see what return does here.

3 Likes

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!

3 Likes

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

You don’t need this. You are returning the value after that.

1 Like

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.

1 Like

Would this value be the value of the color that it’s currently on?

Yeah, I was trying that before I had posted, but didn’t know you had to return it as well. Will keep you guys posted on what occurs next.

Edit: That worked, thanks!

There really is no point of having a function there. You can just remove the function and do:

Part1.BrickColor = BrickColor.palette(1009)
2 Likes

Also, does every function require a return or is it only in certain cases?

Only in certain cases. You don’t always have to return a value from a function.

2 Likes

This is what I would do

	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