[SOLVED] Needing Help to Add Battery System for Flashlight

I am trying to finalize a Battery System for my Flashlight, this System will focus differently because there will be no Battery Bar to tell the Players how much Battery they have got left. Instead, the Brightness of the Flashlight will become futile meaning the Brightness will reduce if the Flashlight is turned on, I am also trying to make Batteries (Tool) scatted around the Map and when you die the Batteries will change position of the Map when the Battery is used the Flashlight will remain neutral again. I already have a kit for the Batteries going into the Players inventory using a Prompt.

Anyway I can do this?

Flashlight Script:

player = game.Players.LocalPlayer
tool = script.Parent

repeat wait() until player

tool.Equipped:connect(function(mouse)
	mouse.Button1Down:connect(function()
		tool.Handle.SpotLight.Enabled = not tool.Handle.SpotLight.Enabled
		tool.Handle.Sound:Play()
	end)
end)```

So to make a flashlight’s brightness decrease alongside its battery level, you could just use the Brightness property from the light source to tell how much battery is left.

For example, each flashlight could begin at brightness 1, and slowly diminish until hitting 0, telling you that the flashlight is out of battery.

To make the “battery level” or brightness property diminish you would just hook up a loop that decreases the brightness by a certain amount while the flashlight is equipped/on.

Here is some very simple code accomplishing that:

local tool = script.Parent
local flashlightActive = false

tool.Activated:Connect(function()
	if not flashlightActive and tool.Handle.SpotLight.Brightness > 0 then --//If flashlight is off and still has battery. (Turning it on)
		flashlightActive = true
		tool.Handle.SpotLight.Enabled = true
		repeat
			tool.Handle.SpotLight.Brightness -= 0.01 --change this based on desired diminish rate
			task.wait(0.1)
		until flashlightActive == false or tool.SurfaceLight.Brightness == 0
	elseif flashlightActive then --//Flashlight is currently on. (Turning it off)
		flashlightActive = false
		tool.Handle.SpotLight.Enabled = false
	else --//Flashlight is out of battery.
		--you dont need this else if you dont want to do anything when the flashlight is dead upon clicking
	end
end)

To recharge you would simply just set the brightness back to the desired level.

Hope this gives you an idea of how to achieve what you’re looking for.

1 Like

@GiveMeABloxy
I have already made a Battery System

how do I make this work?

Battery Script:

tool = script.Parent

repeat wait() until Player

tool.Equipped:connect(function(mouse)
	mouse.Button1Down:connect(function()
	end)

while true do
script.Parent.ClickDetector.MouseClick:Connect(function(Player)
	if Player.Backpack:FindFirstChild("WeakFlashlight") then
		Player.Backpack:FindFirstChild("WeakFlashlight").Handle.Battery.Value = Player.Backpack:FindFirstChild("WeakFlashlight").Handle.Battery.Value + 50
		Player.Backpack:FindFirstChild("WeakFlashlight").Handle.Battery.Value = (Player.Backpack:FindFirstChild("WeakFlashlight").Handle.Battery.Value > 100 and 100 or Player.Backpack:FindFirstChild("WeakFlashlight").Handle.Battery.Value)
		script.Parent:Destroy()
		end
	end
end```

ignore the “ClickDetector” I just realised

So here I assume you’re talking about using batteries to recharge the flashlight. What you have right now is on the right track but the while loop is confusing me, you shouldn’t need a loop to do this.

Here is some code of how you could click with the battery tool to recharge the flashlight:

local player = game.Players.LocalPlayer
local tool = script.Parent

tool.Activated:Connect(function()
	local getFlashlight = player.Backpack:FindFirstChild("WeakFlashlight")
	if getFlashlight then
		getFlashlight.Handle.Battery.Value += 50
		tool:Destroy()
	end
end)

This should work fine, although I’m unsure exactly how you’d want the recharging to work.

1 Like

Quick problem it doesn’t detect to see if the value is at 100 because when it does it doesn’t add any value it will add all value to Flashlight Battery reaches 100 in value.

I see, here is the same code updated with a check which will not allow the battery charge to go over the max charge set:

local player = game.Players.LocalPlayer
local tool = script.Parent

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:Destroy()
	end
end)
1 Like

Thank you so much! you have been a really big help I very much appreciate your help and will keep you Updated if any further touches has been misplaced.

Glad to help & best of luck with your project!

1 Like

@GiveMeABloxy
Hey bro, sorry to bother you and all but how would I destroy the “Battery” after it has been collected?

Script:

local ToolNames = {"Battery"} -- Change Item to your item name and move it to ServerStorage
local Storage = game.ServerStorage.RareItems


local Part = script.Parent
local ProximityPrompt = Part:WaitForChild("ProximityPrompt")

ProximityPrompt.Triggered:connect(function(Player)
	if Player and Player.Character then
		local Backpack = Player:WaitForChild("Backpack")
		for i = 1, #ToolNames do
			local Tool = Storage:FindFirstChild(ToolNames[i])
			if Tool then
				if Backpack:FindFirstChild(Tool.Name) == nil then
					Tool:clone().Parent = Backpack
				end	
			end
		end
	end
end)
-- I didnt coded all the scriot, I just changed it from click to pick up to E to pick up

when “Battery” is picked moved to a different position in the Map.

Script:

local tbl = {}
local folder = workspace.Spawn
local tool = game.Workspace.Battery

	for _,part in pairs(folder:GetChildren()) do
	table.insert(tbl, part.CFrame)
end

local clone = tool:Clone()
tool.CFrame = tbl[math.random(#tbl)] * CFrame.new(0,1,0)
tool.Parent = workspace```

For this I would simplify the code, you don’t need to create a table and add all of the children to the table because :GetChildren() already returns a table with all of it’s children.

Also I noticed that you were setting the CFrame and Parent of the original tool instead of the cloned tool.

Here is some simplified code that should work in terms of creating a new battery at a random spawn:

local folder = workspace.Spawn
local tool = game.Workspace.Battery

local toolSpawns = folder:GetChildren()
local clone = tool:Clone()
clone.CFrame = toolSpawns[math.random(1,#toolSpawns)].CFrame + Vector3.new(0,1,0) --//Picks one random spawn's CFrame with Y offset of 1 stud.
clone.Parent = workspace
1 Like

when I would to pick up the “Battery” it doesn’t change positions

This is the “PickUp” Script to Pick Up the actual Battery and add the “Battery” into the Players Backpack.

Do you just want one battery spawned at all times that changes positions whenever a player picks it up? I’m just trying to understand what you’re going for.

1 Like

Okay, so for instance if the Player picks up the Battery it will re-position the current Battery (the Battery the Player has picked up) and will place the Battery to one of the positions listed as “Spawn”.

Otherwise Players can keep spamming the Battery and they would have infinite Flashlight Battery.

(I was referring to the topic stuff rather than the current stuff)

When equipping the flashlight, have an ObjectValue called CurrentFlashlight, within the flashlight have the amount of battery remaining, if a full battery lasts only 2 minutes, put 120 seconds has the max battery it can hold.
Use RunService to transition smoothly between a full, to empty battery, like so:

-- when RunService is active
local Tool = CurrentFlashlight.Value -- Tool
local CurrentBattery = Tool:GetAttribute("CurrentBattery")
-- the current amount that is within the battery
local FullBattery = Tool:GetAttribute("FullBattery")
-- the amount of time for a full battery

Tool:SetAttribute("CurrentBattery", math.clamp(CurrentBattery - dt, 0, FullBattery)
-- math.clamp will help set a limit to where the battery will go, it cant pass 0
-- but it also cant pass the FullBattery capacity.

-- DeltaTime is the time between each frame, you can use it to subtract from
-- your Current Battery until you reach 0

local Percentage = math.floor(Tool:GetAttribute("CurrentBattery")/FullBattery)*100
-- percentage of the Battery
-- might be useful for UI purposes

Because this a Client related visual effect, keep this on the Client, but if you want to prevent exploitation, have the Server confirm current values.

1 Like

You could add these two functions for giving the player the battery and then moving it to a random spawn:

local folder = workspace.Spawn
local Tool = game.Workspace.Battery

local function pickupBattery()
	local batteryClone = Tool:Clone()
	batteryClone.Parent = player.Backpack
end

local function moveBattery()
	local toolSpawns = folder:GetChildren()
	Tool.CFrame = toolSpawns[math.random(1,#toolSpawns)].CFrame + Vector3.new(0,1,0) --//Picks one random spawn's CFrame with Y offset of 1 stud.
end

ProximityPrompt.Triggered:connect(function(Player)
	if Player and Player.Character then
		local Backpack = Player:WaitForChild("Backpack")
		for i = 1, #ToolNames do
			local Tool = Storage:FindFirstChild(ToolNames[i])
			if Tool then
				if Backpack:FindFirstChild(Tool.Name) == nil then
					moveBattery()
					pickupBattery()
				end	
			end
		end
	end
end)

I generally wouldn’t recommend storing tools in the workspace but if you do make sure you unanchor the tool so that the player can properly equip it.

1 Like

@GiveMeABloxy

local ToolNames = {"Battery"} -- Change Item to your item name and move it to ServerStorage
local Storage = game.ServerStorage.RareItems


local Part = script.Parent
local ProximityPrompt = Part:WaitForChild("ProximityPrompt")

local folder = workspace.Map.Zones.BatteryZone
local tool = game.Workspace.Map.KillArea.Batteries.Battery

local function pickupBattery()
	local batteryClone = Tool:Clone()
	batteryClone.Parent = Tool.Backpack
end

local function moveBattery()
	local toolSpawns = folder:GetChildren()
	Tool.CFrame = toolSpawns[math.random(1,#toolSpawns)].CFrame + Vector3.new(0,1,0) --//Picks one random spawn's CFrame with Y offset of 1 stud.
end

ProximityPrompt.Triggered:connect(function(Player)
	if Player and Player.Character then
		local Backpack = Player:WaitForChild("Backpack")
		for i = 1, #ToolNames do
			local Tool = Storage:FindFirstChild(ToolNames[i])
			if Tool then
				if Backpack:FindFirstChild(Tool.Name) == nil then
					moveBattery()
					pickupBattery()
				end	
			end
		end
	end
end)
-- I didnt coded all the scriot, I just changed it from click to pick up to E to pick up