How to make a part color change repeatedly

  1. What do you want to achieve? hello am new to scripting, and i can’t make my neon part colour change repeatedly
local a = script.Parent.a
local b = script.Parent.b
local c = script.Parent.c
local d = script.Parent.d
local al = a.PointLight
local bl = b.PointLight
local cl = c.PointLight
local dl = d.PointLight
local colors = {red = Color3.fromRGB(255, 0, 0), green = Color3.fromRGB(40, 127, 71),}
local lcolors = { redl = Color3.fromRGB(130, 27, 27), greenl = Color3.fromRGB(0, 170, 0)}
local randomlcolor = math.random(1,lcolors)
local randomcolor = math.random(1,colors)
while true do
	a.Color = randomcolor
	al.Color = randomlcolor
	task.wait(0.1)
	b.Color = randomcolor
	bl.Color = randomlcolor
	task.wait(0.1)
	c.Color = randomcolor
	cl.Color = randomlcolor
	task.wait(0.1)
	d.Color = randomcolor
	dl.Color = randomlcolor
	task.wait(0.1)
end

am trying to make the lights for the part and the part itself change colour there are exactly 4 parts and 4 spot lights, anything that has an l in it it is for the light and am also trying to make it randomly change forever, thanks

2 Likes

you need to do this:

while true do
    local randomlcolor = math.random(1,lcolors)
    local randomcolor = math.random(1,colors)
	a.Color = randomcolor
	al.Color = randomlcolor
	task.wait(0.1)
	b.Color = randomcolor
	bl.Color = randomlcolor
	task.wait(0.1)
	c.Color = randomcolor
	cl.Color = randomlcolor
	task.wait(0.1)
	d.Color = randomcolor
	dl.Color = randomlcolor
	task.wait(0.1)
end

also I would use a for loop

and this code doesn’t even work, you’re changing it to a number
wait I’m gonna redo the code

1 Like
local a = script.Parent.a
local b = script.Parent.b
local c = script.Parent.c
local d = script.Parent.d
local al = a.PointLight
local bl = b.PointLight
local cl = c.PointLight
local dl = d.PointLight

local parts = {a,b,c,d}
local lights = {al,bl,cl,dl}

local colors = {Color3.fromRGB(255, 0, 0),Color3.fromRGB(40, 127, 71),}
local lcolors = {Color3.fromRGB(130, 27, 27),Color3.fromRGB(0, 170, 0)}

while true do
	for i = 1,#parts do
		local part = parts[i]
		local light = lights[i]
		local randomPartColor = colors[math.random(1,#colors)]
		local randomLightColor = lcolors[math.random(1,#lcolors)]
		part.Color = randomPartColor
		light.Color = randomLightColor
		task.wait(0.1)
	end
end
1 Like
local colors = {Color3.fromRGB(255, 0, 0), Color3.fromRGB(40, 127, 71)} -- define colors
local lcolors = {Color3.fromRGB(130, 27, 27), Color3.fromRGB(0, 170, 0)} -- define light colors

-- colors and lcolors are arrays because there is no key defined (i.e. {hello = "world"})

local parts = {script.Parent.a, script.Parent.b, script.Parent.c, script.Parent.d} -- get the parts
local lights = {} -- to be filled

for i,v in parts do -- loop through all the parts (a,b,c,d)
	if v:FindFirstChildOfClass("PointLight") then -- if the part has a pointlight
		lights[v] = v:FindFirstChildOfClass("PointLight") -- add the light to the lights table
	end
end

while true do -- while loop
	for i,v in parts do -- loop through parts
		local randomColor, randomLightColor = math.random(#colors), math.random(#lcolors) -- get colors
		v.Color = randomColor -- assign color
		if lights[v] then -- check if a light exists
			lights[v].Color = randomLightColor -- assign the light color
		end
		task.wait(.1) -- wait 0.1s
	end
	task.wait(.1) -- wait 0.1s
end

This should fix the problem. Be sure to learn from this code so your scripting skills become better! I labeled all the lines so you understand whats going on.

1 Like

thank you SOOO much and i honestly cant thank you enough for this <3

1 Like

i appreciate the time and help dude cant thank you enough for this

1 Like

oh wait sorry to bother you but it showed me an error it says (Unable to assign property Color. Color3 expected, got number)

oh wait hang on i think i got this

local a = script.Parent.a
local b = script.Parent.b
local c = script.Parent.c
local d = script.Parent.d
local al = a.PointLight
local bl = b.PointLight
local cl = c.PointLight
local dl = d.PointLight

local colors = {
	red = Color3.fromRGB(255, 0, 0),
	green = Color3.fromRGB(40, 127, 71),
}

local lcolors = {
	redl = Color3.fromRGB(130, 27, 27),
	greenl = Color3.fromRGB(0, 170, 0)
}


function getRandomColor(colorTable : {}) : Color3
	local keys = {} 
	for key in pairs(colorTable) do
		table.insert(keys, key)
	end
	local randomKey = keys[math.random(1, #keys)]
	return colorTable[randomKey]
end

while true do
	local randomcolor = getRandomColor(colors)
	local randomlcolor = getRandomColor(lcolors)
	
	a.Color = randomcolor
	al.Color = randomlcolor
	task.wait(0.1)
end
1 Like

Oh yeahhhh mb :skull:

local randomColor, randomLightColor = math.random(#colors), math.random(#lcolors) -- get colors
v.Color = colors[randomColor] -- assign color
if lights[v] then -- check if a light exists
	lights[v].Color = lcolors[randomLightColor ] -- assign the light color
end
1 Like

nvm i dont got it the error was at

		v.Color = randomColor -- assign color

again tysm dude am forced to say more words blah blah blah later!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.