Need some advice in regards to a Lighting System

Is there any advice that I can get on how to improve my script (This is for a Emergency Vehicle/Police Car Lighting System)

local function on(p)
	p.Transparency = 0.02
	if p:FindFirstChild("Light") then
		p.Light.Enabled = true
		p.Light.Color = p.Color
	end
end
local function off(p)
	p.Transparency = 1
	if p:FindFirstChild("Light") then
		p.Light.Enabled = false
		p.Light.Color = p.Color
	end
end

Thanks all!

1 Like

So first is there a problem? if so tell us, if not move it to #help-and-feedback:code-review

There, just moved it, my bad.

Thanks for telling me!

Although your code seems a little repetitive, I think it’s fine because it’s such a small script and your two functions have a clear name and purpose.

p.Light.Color = p.Color is redundant because you’re setting it to the same thing in both functions.

Personally, this is how I would have done it:

local function adjustLight(p, transparency, enabled)
    p.Transparency = transparency
    if p:FindFirstChild("Light") then
		p.Light.Enabled = enabled
	end
end

local function on(p)
    adjustLight(p, 0.02, true)
end

local function off(p)
    adjustLight(p, 1, false)
end