Why wont this value go up?

Hello! So I have a flashlight in my game and I have a flashlight battery in it. I have it so if you find batteries your flashlight battery goes up though its not working? I also want it so the player who found the battery gets the flashlight battery

LocalScript - Flashlight Script

local UIS = game:GetService("UserInputService")
local BadgeService = game:GetService("BadgeService")

local Debouce = false
local Player = game.Players.LocalPlayer

	script.Parent.FlashlightScript.FlashlightBattery.Value = 100

while wait(0.1) do
	local BatteryGui = game.Players.LocalPlayer.PlayerGui.BatteryGui
	if script.FlashlightBattery.Value < 25 then
		BatteryGui.BatteryStatus.Image = "rbxassetid://11444586302"
	elseif script.FlashlightBattery.Value < 50 then
		BatteryGui.BatteryStatus.Image = "rbxassetid://11444607186"
	elseif script.FlashlightBattery.Value < 75 then
		BatteryGui.BatteryStatus.Image = "rbxassetid://11444611852"
	elseif script.FlashlightBattery.Value < 100 then
		BatteryGui.BatteryStatus.Image = "rbxassetid://11444616312"
	elseif script.FlashlightBattery.Value < 0 then
		BatteryGui.BatteryStatus.Image = ""

Script - Battery


script.Parent.ProximityPrompt.Enabled = false

script.Parent.Transparency = 0.5



player.Backpack.Flashlight.FlashlightScript.FlashlightBattery.Value = 100


script.Parent.ProximityPrompt.Enabled = true

script.Parent.Transparency = 0


Remove the event for the client, since you’re changing it from the server anyway, and what’s the code for removing power?

removing script:

	script.Parent.Front:WaitForChild("SurfaceLight").Enabled = true
	script.Parent.Light:WaitForChild("Light").Enabled = true
	script.Parent.Light:WaitForChild("Shadow").Enabled = true
	if script.Parent.FlashlightScript.FlashlightBattery.Value == 0 then
		game:GetService("BadgeService"):AwardBadge(player.UserId, 2129270521)

	script.Parent.Front:WaitForChild("SurfaceLight").Enabled = false
	script.Parent.Light:WaitForChild("Light").Enabled = false
	script.Parent.Light:WaitForChild("Shadow").Enabled = false

while wait(1) do
	if script.Parent.Front.SurfaceLight.Enabled == true then
		if script.Parent.FlashlightScript.FlashlightBattery.Value > 0 then
			script.Parent.FlashlightScript.FlashlightBattery.Value -= 1
			script.Parent.Front:WaitForChild("SurfaceLight").Enabled = false
			script.Parent.Light:WaitForChild("Light").Enabled = false
			script.Parent.Light:WaitForChild("Shadow").Enabled = false

i tried this before but the thing is if the player is holding the flashlight then I get this error

 Flashlight is not a valid member of Backpack "Players.Player.Backpack"

Oh, that’s because when it is equipped, it’s under the Character.

how would I make it so the script can check then?

You can do this several ways (I don’t know which is best, so choose the one you prefer I guess)

  1. Bindable Event (Server to Server)
  2. Object Value (Stores the Flashlight Instance, which you can put under a folder, that isn’t named leaderstats, that is under the Player)
  3. Loop though all the battery Instances in the removing script, and detect proximity prompts from them, check that the Player doing the prompt is the Player they use (We can use this with CollectionService and tags)
Other notes

I highly recommend using CollectionService to loop through all battery instances, from a script in ServerScriptSerivce, for easier readability, management, and I think performance, this method works best with options 1 and 2.

hmm okay thank you! One question though. Is there a way to check if the tool is in the character and if it isnt then it checks the backpack?

You could use :FindFirstChild() On both the backpack and the character.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.