I need help with a light system

So, I’m making a system that when you click on a button it makes a Spotlight brightness go higher until it reach the value of 0.4 then if you click again it make the brightness go darker until it reach 0

But it doesn’t work at all and doesn’t trow any error, so I can’t fix it
I’m pretty sure the error come from these two code block

		while Light.Brightness > 0.4 do
			Light.Brightness = Light.Brightness + 0.01
			wait(0.01)
		end
-------------------------------------------------------------
		while Light.Brightness < 0 do
			Light.Brightness = Light.Brightness - 0.01
			wait(0.01)
        end

Anyway, code is below:

local Model = script.Parent
local Button = Model.Button.ClickDetector
local LightModel = Model.Model
local LightPart = LightModel.Light
local Light = LightModel.Light.SpotLight
local Value = Model.IsOn.Value

Button.MouseClick:Connect(function()
	if Value == false then
		LightPart.Material = Enum.Material.Neon
		while Light.Brightness > 0.4 do
			Light.Brightness = Light.Brightness + 0.01
			wait(0.01)
		end
		Value = true
	else
		while Light.Brightness < 0 do
			Light.Brightness = Light.Brightness - 0.01
			wait(0.01)
		end
		Value = false
		LightPart.Material = Enum.Material.SmoothPlastic
	end
end)

Some screenshot if neede:
Capture d’écran 2022-05-03 202312

Any help is really appreciated right now.

Hey, you’re code is fine it’s just the logic in the loops. All you need to do is reverse the greater than sign and less than signs in the loops. Check if the brightness is < 0.4 the count up and if the brightness > 0 the count down.

– # Brightness less than 0.4

while Light.Brightness < 0.4 do
	  Light.Brightness += 0.01
	  wait(0.01)
end

– # Brightness greater than 0

while Light.Brightness > 0 do
	  Light.Brightness -= 0.01
	  wait(0.01)
end
1 Like

local Model = script.Parent
local Button = Model.Button.ClickDetector
local LightModel = Model.Model
local LightPart = LightModel.Light
local Light = LightModel.Light.SpotLight
local Value = Model.IsOn.Value

Button.MouseClick:Connect(function()
if Value == false then
LightPart.Material = Enum.Material.Neon
repeat
wait(0.01)
Light.Brightness = Light.Brightness + 0.01
until
Light.Brightness == 0.4

	Value = true
else
	repeat
		Light.Brightness = Light.Brightness - 0.01
		wait(0.01)

until
Light.Brightness == 0

	Value = false
	LightPart.Material = Enum.Material.SmoothPlastic
end

end)

1 Like

Sorry for the messy script im currently on mobile

1 Like

Thanks for the help and the explaining. :grin:

Sorry yours didn’t work but thanks anyways :grin: repeat loops is a good idea