Part not getting color from SurfaceLight

Hello, I am trying to script something so the part will have the same color as a light.
When you changed the color of the light it should automatically change the color of the part. But it’s not changing the color.

while true do
	script.Parent.Color = script.Parent.SurfaceLight.Color
	wait(.01)
end

As you can see this is a simple loop.

If you’re not getting any errors, I’m assuming that the script that changes the color is client-side. If a client or LocalScript attempts to change something, only that player/client will see that change. This means the server script won’t detect the color change. Otherwise, it could just be that you’re not waiting for everything to load.

Either way, I’d recommend using an event instead of a loop. Example:

local part = script.Parent
local light = part:WaitForChild('SurfaceLight')
light.Changed:connect(function()
	part.Color = light.Color
end)

For fun, you can add this to the bottom of that script to see it in action:

--Rainbow lights
local speed = 0.2
local T = game:GetService('TweenService')
function tween(o,t,l,s,d)
	if not s then s = Enum.EasingStyle.Linear end
	if not d then d = Enum.EasingDirection.InOut end
	local i = TweenInfo.new(l,s,d)
	return T:Create(o,i,t)
end
local colors = {Color3.new(1,0,0),Color3.new(1,1,0),Color3.new(0,1,0),Color3.new(0,1,1),Color3.new(0,0,1),Color3.new(1,0,1)}
repeat
	for _,v in pairs(colors) do
		tween(light,{Color=v},speed):Play()
		wait(speed)
	end
until false

(Ignore that I’m using an entire tween function for one basic tween, I always use it.)

1 Like