-- limit battery to be in a range of 0-100
getFlashlight.Handle.Battery.Value = math.clamp(getFlashlight.Handle.Battery.Value + rechargeRate, 0, maxCharge)
this might not be the solution but its worth the try
To answer your question, there is NO other scripts that decrease the Battery percentage expect the Flashlight Script which databases the Flashlight Value when the Flashlight is active.
I still don’t quite understand what do you mean by “it only increases then it decreases afterwards”; do you mean that the tool recharges the flashlight and then decreases the charge after recharging?
So, well which the Player will click freely and Battery Value will increase to 100% when Flashlight in use the Value will decrease every 1.5 seconds in Value.
local tool = script.Parent.Parent
local batteryValue = tool.Handle.Battery
local equipped = false
local isOn = false
local configs = tool.Settings
local event = nil
local function map(n, start, stop, newStart, newStop, withinBounds)
local value = ((n - start) / (stop - start)) * (newStop - newStart) + newStart
if not withinBounds then
return value
end
if newStart < newStop then
return math.max(math.min(value, newStop), newStart)
else
return math.max(math.min(value, newStart), newStop)
end
end
if game.ReplicatedStorage:FindFirstChild("kFlashlightEvent") == nil then
local kevent = Instance.new("RemoteEvent", game.ReplicatedStorage)
kevent.Name = "kFlashlightEvent"
event = kevent
else
event = game.ReplicatedStorage.kFlashlightEvent
end
tool.Equipped:Connect(function()
equipped = true
tool.Handle.EquipSound:Play()
end)
tool.Unequipped:Connect(function()
equipped = false
tool.Handle.EquipSound:Play()
end)
local function turnOn()
tool.Handle.SpotLight.Enabled = true
isOn = true
end
local function turnOff()
tool.Handle.SpotLight.Enabled = false
isOn = false
end
local function onToggle(playerName)
if tostring(playerName) == tool.Parent.Name then
tool.Handle.ClickSound:Play()
if isOn then
turnOff()
else
if batteryValue.Value > 0 then
turnOn()
end
end
end
end
event.OnServerEvent:Connect(onToggle)
while batteryValue.Value > 0 do
if equipped and isOn then
batteryValue.Value = batteryValue.Value - 1
tool.Handle.SpotLight.Brightness = map(
math.clamp(batteryValue.Value, 10, 40),
10, 40,
configs.MinimumBrightness.Value, configs.MaximumBrightness.Value)
if batteryValue.Value == 0 then
turnOff()
break
end
end
wait(configs.BatteryRate.Value)
end```
you can try adding an attribute that works as a debounce to disallow the script from decreasing the battery whilst recharging
while batteryValue.Value > 0 do
if equipped and isOn and not tool:GetAttribute("isCharging") then
tool.Activated:Connect(function()
local getFlashlight = player.Backpack:FindFirstChild("WeakFlashlight")
local rechargeRate = 50
local maxCharge = 100
if getFlashlight then
getFlashlight:SetAttribute("isCharging", true)
if (getFlashlight.Handle.Battery.Value + rechargeRate) <= maxCharge then
getFlashlight.Handle.Battery.Value += rechargeRate
else
getFlashlight.Handle.Battery.Value = maxCharge
end
tool.Handle.ClickSound:Play()
wait (0.25)
getFlashlight:SetAttribute("isCharging", false)
tool:Destroy()
end
end)