How do i fix this script only turing on 1 light, then turing on the next when the batter gets to 0

	if HasBat.Value == true then
		local BatVal = script.Parent.Parent:FindFirstChild("BatInUse").BatteryPercent
		Lightson = true
		for _, model in pairs(script.Parent.Parent:GetDescendants()) do
			if model:IsA("Model") and model.Name == "HeadLights" then
				warn("found another model called headlights")

				for _, partInModel in pairs(model:GetDescendants()) do
					if partInModel:IsA("BasePart") and partInModel.Name == "Light" then
						print("turning part to neon", partInModel)
						if partInModel.Material == Enum.Material.Neon then
							partInModel.Material = Enum.Material.Plastic
							partInModel.SurfaceLight.Enabled = false
							Lightson = false
						else
							partInModel.Material = Enum.Material.Neon
							partInModel.SurfaceLight.Enabled = true
							
						end
						
					end
					while Lightson == true and BatVal.Value > 0 do
						task.wait(0.05)
						BatVal.Value = BatVal.Value - 1
						if BatVal.Value == 0 then
							Lightson = false
							partInModel.Material = Enum.Material.Plastic
							partInModel.SurfaceLight.Enabled = false
						end
					end	
					
				end
			end
		end
		
	end
end)

This script slowly depletes a battery while the lights are on, but it does it for 1 light, then does it for the next. how do i do it for both lights at once?

You can use task.spawn to make this run at the same time as the rest of your code.
Example:

task.spawn(function()
	while true do
		print("While loop running")
		task.wait(1)
	end
end)
print("I still print!")

do i just put this where i had my old ode? and then put my old code inside it?

You’ll need to put the while loop I quoted inside task.spawn like this:

task.spawn(function()
	while Lightson == true and BatVal.Value > 0 do
		-- ...
	end
end)

i tried that, and now as soon as i start the function, the battery percent goes to -23 exacly instantly and the lights dont turn off

ok i fixed it, but now the battery stops at -1 with 2 lights

You can check if the battery is already depleted before subtracting 1 from it, like this:

now when i press the button to turn on lights, one of them turns on even when the battery is at 0

It doesn’t look like you’re checking if the battery is above zero until after turning on the light, so try this:

if HasBat.Value == true then
	local BatVal = script.Parent.Parent:FindFirstChild("BatInUse").BatteryPercent
	if BatVal.Value <= 0 then
		return
	end
	-- ...

how do i implement this into the current script?

Add

	if BatVal.Value <= 0 then
		return
	end

below

if HasBat.Value == true then
	local BatVal = script.Parent.Parent:FindFirstChild("BatInUse").BatteryPercent