Script to change part color not working

I have a script that is supposed to make a part change colors in a rainbow, but the way I implemented it doesn’t seem to work.

local function pcolor()
	
	local x = 0

	Color3.new(Color3.fromHSV(x,1,1))
		x = x + 2/255
		if x >=1 then
			x=0
		end
		wait()
	end

local startColor = pcolor

Are there any mistakes in the code?

You need to call the function for the code inside of it to work.

1 Like
  1. You are not executing your function

  2. There is no loop in function for it to keep changing color. It will change color once.

  3. You could use BrickColor.random()

Let me know if you have any questions

1 Like
local function pcolor()
	
	local x = 0
	
	while true do
	Color3.new(Color3.fromHSV(x,1,1))
		x = x + 2/255
		if x >=1 then
			x=0
		end
		wait()
	end

local startColor = pcolor()

Is this better?

You don’t really need to make it a function but if you want to

``
local Brick = your brick path
function pcolor()

local x = 0

while true do
Brick.Color3 = Color3.new(Color3.fromHSV(x,1,1))
	x = x + 2/255
	if x >=1 then
		x=0
	end
	wait()
end

pcolor()
``

I switched to this, but it still doesn’t work

local startColor = Color3.new(math.random(0,1))

Local Part = game.Workspace.”PartName”

If part.color = color3.fromRGB(“yourcurrentpartcolor”) then
Part.color = Color3.New(“NewColor”)

Switched to this

local x, y, z = math.random(0,1), math.random(0,1), math.random(0,1)

local startColor = Color3.new(x, y, z)

Any way for it to continuously change?
Also, any way to make the numbers between 0,1 appear more specific (EX: 0.37)

Wym change numbers and such? I’m a little confused on what you were asking sorry.

The part is meant to change color after each inital press, and “startColor” is the color it goes to.

But i can’t seem to make it change

Insert Part —> Workspace —> Script

Local Part = game.Workspace.Part

if Part.Color == color3.fromRGB(255,255,255) then

Replace Color3.fromRGB with your first part color

Part.Color = Color3.New(20,20,20)

Replace Color3.new with your own colors.

Sorry this is not copy and paste it’s all typed from chat, some info may be a little off.

While Wait() Do
Game.Workspace.Part.BrickColor = BrickColor.random()

I think this should work but not 100% if this is what you mean.

local function pcolor()
	
	local x = 0

	Color3.new(Color3.fromHSV(x,1,1))
		x = x + 2/255
		if x >=1 then
			x=0
		end
		wait()
	end

part.Color = pcolor()

Well if you are clicking it would be something like…

Script.Parent.ClickDetector:Connect(Function()
If script.parent.clickdetector then
If clicked then
Part.BrickColor = BrickColor.random()

I think something like this maybe?

Legit copy and pasted the other dudes stuff above.

Here’s the entire code.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local lightModel = workspace.LEDBox
local lights = {
	lightModel.LED1.led,
	lightModel.LED2.led,
	lightModel.LED3.led,
	lightModel.LED4.led,
	lightModel.LED5.led,
	lightModel.LED6.led,
	lightModel.LED7.led,
	lightModel.LED8.led,
	lightModel.LED9.led
}

local x, y, z = math.random(0,1), math.random(0,1), math.random(0,1)

local startColor = Color3.new(x, y, z)

local endColor = Color3.new(0, 0, 0)

local fadeTime = 0.3

local lightAnimations = {}

for _, light in lights do
	lightAnimations[light] = TweenService:Create(light, TweenInfo.new(fadeTime),{Color = endColor})
end

function fade(light)
	local tween = lightAnimations[light]

	if lightAnimations[light] then
		if tween.PlaybackState == Enum.PlaybackState.Playing then
			tween:Cancel()
		end

		tween:Play()
	end
end

game.ReplicatedStorage.LightStart.OnServerEvent:Connect(function(player, light)
	local tween = lightAnimations[light]

	if lightAnimations[light] then
		if tween.PlaybackState == Enum.PlaybackState.Playing then
			tween:Cancel()
		end
	end

	light.Color = startColor
end)

game.ReplicatedStorage.LightStop.OnServerEvent:Connect(function(player, light)
	fade(light)
end)

It’s meant to change the part’s color using a key bind, then applying an animation so it fades back to “endColor”

That’s why I want “startColor” to change every time I do press it.

Use the random function, your script just needs some minor tweaks man.

I don’t understand your local X, Y, Z exactly especially if you’re wanting a random color. You only need to call the function once not 3 different math.randoms

It is meant to go between 0,0,0 and 1,1,1 each press, which I can’t get specific numbers, like this: 0.37, 0.45, 1

But for a part color (0,0,0) and (1,1,1) is nothing that’s not even a color change really you’re (0,0,0) to (255,255,255)