Togglable light switch

I’m trying to make a bedside lamp for a cozy, little, house but the code is not working.

The issue is, is that when I click, it toggles and then untoggles.

I have tried using while true do, and changing certain aspects of the code. I have also searched through the devforum, but have found nothing.

If it helps, this is the code.

local clickDetector = script.Parent.Parent.LampCover.ClickDetector
local light = script.Parent.PointLight
local OnOffSound = script.Parent.OnOff
local IsOn = script.Parent.IsOn

light.Brightness = 0 
IsOn = false
print("Success")

function onMouseClick()
	if IsOn == false then
		IsOn = true
		print("Success")
		
	end
	
	if IsOn == true then
		IsOn = false
		print("Success")
	end
end

function onClick(mouse)
	OnOffSound:Play()
end

clickDetector.MouseClick:Connect(onMouseClick)
clickDetector.MouseClick:Connect(onClick)

while true do
	wait(0.2)
	if IsOn == false then
		light.Brightness = 0 else
		light.Brightness = 1.5
	end
end

Please tell me what to change and provide a better script if you can. Also, if you know a devforum post that might help me, please provide that instead. Thank you very much!

local clickDetector = script.Parent.Parent.LampCover.ClickDetector
local light = script.Parent.PointLight
local OnOffSound = script.Parent.OnOff

local IsOn = false 

clickDetector.MouseClick:Connect(function()
	if IsOn == false then
		IsOn = true
		light.Brightness = 1.5
		OnOffSound:Play()
		print(IsOn)
	else 
		IsOn = false
		light.Brightness = 0
		OnOffSound:Play()
	end
end)

Try this, let me know if it doesn’t work.

So the light part of the script works, but not the sound. Should I have that in a separate script? Oh, and what should I have as the RollOffMaxDistance and RollOffMinDistance?

The sound should still work in that script. Is there any error messages? Also make sure the volume is high enough so you can hear it.

RollOffMaxDistance and RollOffMinDistance can be anything, the lower it is, the closer you need to be to hear it.

local part = script.Parent
local model = part.Parent
local lamp = model.LampCover
local click = lamp.ClickDetector
local sound = part.OnOff
local light = part.PointLight

local isOn = false

local function onMouseClick()
	isOn = not isOn
	sound:Play()
	light.Brightness = isOn and 1.5 or 0
end

click.MouseClick:Connect(onMouseClick)