Confusing Tool Glitch

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m making a torch with battery for my horror game.

  2. What is the issue? Include screenshots / videos if possible!


    Even though I have a script to drain the battery, it stays the same.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have asked ChatGPT and tried different methods of a forever loop.

My LocalScript

local Flashlight = script.Parent
local Battery = Flashlight:WaitForChild("Battery")
local Active = false

Flashlight.Equipped:Connect(function()
	script.Parent.Enabled = true
end)

Flashlight.Unequipped:Connect(function()
	script.Parent.Enabled = false
end)

Flashlight.Activated:Connect(function()
	Active = true
end)

Flashlight.Deactivated:Connect(function()
	Active = false
end)

task.spawn(function()
	while true do
		if Active and Battery.Value > 0 then
			Battery.Value -= 5
			print("Battery:", Battery.Value)
		end
		task.wait(1)
	end
end)
1 Like

You are never using Battery.Value to change the properties of Flashlight.

What do you mean by this? I haven’t made the Flashlight turn off yet. Right now I just want the value to get lower.

You are setting the value correctly, so I assume this is happening because Active is false. Before the if statement, try printing Active. Also try adding printing something inside the function connected to Activated and Unactivated.

Hello, can you try doing Battery.Value = Battery.Value - 5?

Notice that the battery will only drain if you hold click. This is intended behavior from your script.

From what I understand, nothing in my script seems to indicate holding down, as it only changes the active variable when the tool is activated or unactivated?

I have just found my issue! Thanks for the help everyone :slight_smile:

Tool.Deactivated fires when the player releases their click while the tool is equipped and activated

https://create.roblox.com/docs/reference/engine/classes/Tool#Deactivated