You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I’m making a flashlight for my horror game. I already have a working system for draining battery. -
What is the issue? Include screenshots / videos if possible!
When I try to use a battery to regain some lost power, it thinks the flashlight has full battery. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have asked ChatGPT
I have tried SO MANY different methods of adding back to the value.
Drain Battery LocalScript
local Flashlight = script.Parent
local Battery = Flashlight:WaitForChild("Battery")
local Light = Flashlight.Handle:WaitForChild("SpotLight")
local plr = game.Players.LocalPlayer
-- Wait for GUI to be ready
local bar = plr:WaitForChild("PlayerGui"):WaitForChild("FlashlightStats").Background.Bar
local Active = false
local debounce = false
-- GUI visibility + equip sound
Flashlight.Equipped:Connect(function()
plr.PlayerGui.FlashlightStats.Enabled = true
Flashlight.Handle.equip:Play()
end)
Flashlight.Unequipped:Connect(function()
plr.PlayerGui.FlashlightStats.Enabled = false
Active = false
Light.Enabled = false
end)
-- Toggle flashlight ON/OFF
Flashlight.Activated:Connect(function()
if debounce then return end
debounce = true
Flashlight.Handle.Sound:Play()
if Battery.Value > 0 then
Active = not Active
Light.Enabled = Active
print("Flashlight Active:", Active)
else
print("Battery is empty.")
end
task.wait(0.3)
debounce = false
end)
-- Battery drain loop
task.spawn(function()
while true do
task.wait(1) -- 1 unit per second
if Active and Battery.Value > 0 then
Battery.Value -= 1
print("Battery:", Battery.Value)
-- Update UI bar
bar.Size = UDim2.new(0, (Battery.Value / 100 * 200), 1, 0)
-- Flicker if low
if Battery.Value <= 20 then
Light.Enabled = false
task.wait(0.1)
Light.Enabled = true
end
-- Force shutdown if dead
if Battery.Value <= 0 then
Battery.Value = 0
Active = false
Light.Enabled = false
print("Battery dead. Flashlight off.")
end
end
end
end)
Add Battery ServerScript
local remote = game:GetService("ReplicatedStorage"):WaitForChild("EKeyPressed")
local flashlightTemplate = game:GetService("ReplicatedStorage").Items:WaitForChild("Flashlight")
local Hovering = false
local AlertEvent = game.ReplicatedStorage.Remotes.Alert
local function typewriter(object, text, length)
for i = 1, #text, 1 do
object.Text = string.sub(text, 1, i)
wait(length)
end
end
-- Hover Enter
script.Parent.Body.ClickDetector.MouseHoverEnter:Connect(function(plr)
Hovering = true
script.Parent.Highlight.Enabled = true
script.Parent.Prompt.Enabled = true
end)
script.Parent.Label.ClickDetector.MouseHoverEnter:Connect(function(plr)
Hovering = true
script.Parent.Highlight.Enabled = true
script.Parent.Prompt.Enabled = true
end)
-- Hover Leave
script.Parent.Body.ClickDetector.MouseHoverLeave:Connect(function(plr)
Hovering = false
script.Parent.Highlight.Enabled = false
script.Parent.Prompt.Enabled = false
end)
script.Parent.Label.ClickDetector.MouseHoverLeave:Connect(function(plr)
Hovering = false
script.Parent.Highlight.Enabled = false
script.Parent.Prompt.Enabled = false
end)
-- E Key Pressed
remote.OnServerEvent:Connect(function(plr)
if Hovering then
local flashlight = plr.Backpack:FindFirstChild("Flashlight") or plr.Character:FindFirstChild("Flashlight")
if not flashlight then
AlertEvent:FireClient(plr, "No Flashlight")
return
end
local battery = flashlight:FindFirstChild("Battery")
if not battery then
return
end
if battery.Value < 100 then
if battery.Value + 10 >= 100 then
repeat
battery.Value += 1
until battery.Value == 100
end
end
if battery.Value == 100 then
AlertEvent:FireClient(plr, "Full Flashlight")
end
else
AlertEvent:FireClient(plr, "Full Flashlight")
end
end)