[SOLVED] Why isn't the Value Increasing?

So, I am trying to make the Flashlights Value Battery increase once the Player interacts with the Item. This is the current Script
Local Script –

local tool = script.Parent
local equipped = false

tool.Equipped:Connect(function()
	equipped = true
	tool.Handle.EquipSound:Play()
end)


tool.Unequipped:Connect(function()
	equipped = false
	tool.Handle.EquipSound:Play()
end)

tool.Activated:Connect(function()
	local getFlashlight = player.Backpack:FindFirstChild("WeakFlashlight")
	local rechargeRate = 50
	local maxCharge = 100
	if getFlashlight then
		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)
		tool:Destroy()
	end
end)```
6 Likes

But for some reason it only increases then it decreases afterwards why is this happening?

3 Likes

I don’t see a problem here but instead of

you can do

-- 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

2 Likes

I will test right now, thank you.

1 Like

Is there an external script thats decreasing the battery?

2 Likes

It does increase but then it decreases.

1 Like

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.

ok, thats really weird; can you print the getFlashlight.Handle.Battery.Value after the battery value is changed and tell me what does it say?

1 Like

I can make a Kit so you can do breakpoints or if possible even find the issue.

1 Like

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?

yeah basically

Maxum characters

ezgif-1-c1d9d771b8

so; is the charge not supposed to decrease after use? and does it only decrease after you use the battery tool?

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.

oh sorry, i didn’t notice the battery decreasing; can you show me the script that decreases the charge value?

The Script doesn’t decrease the Charge Value but I will proceed to show you.

Script

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)
1 Like

Okay so what happens id, it increases but then after 1 or 2 seconds it will decrease to the current Battery Value.

At this point the Value doesn’t even decrease.