[SOLVED] Why isn't the Value Increasing?

I do have an idea of why this may be caused. So you all thought that the error was in the code but I don’t think it is. Let me explain:

The script in which you decrease the battery is a ServerSided script, meaning the server is changing all the values.
Though now, the battery script which increases the battery is a ClientSided script, meaning it runs on the client locally.
Why is that a problem? You are working with a shadow value. Both the server and the client have their own battery values but only the battery value of the server matters.
How can you fix it?
Simply make the battery script that is supposed to recharge the flashlight a “Script” and not a “LocalScript”.

I hope this helped!

1 Like

20:46:29.644 Players.BaconYTAltb.Backpack.Battery.Script:17: attempt to index nil with 'Backpack' - Server - Script:17

Battery Scritpt:

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

Where this may occur you may ask?

It occurs in this Script:

local ToolNames = {"Battery"} -- Naming the Variable.
local Storage = game.ServerStorage.RareItems -- Finding the "Battery".


local Part = script.Parent -- Detecting the "Battery" as an Parent.
local ProximityPrompt = Part:WaitForChild("ProximityPrompt") -- Calling the Prompt as an functional input.


ProximityPrompt.Triggered:connect(function(Player) -- Functioning the Prompt when triggered.
	Part.EquipSound:Play() -- Calling the Sound into the Part.
	if Player and Player.Character then -- Detecing if the function is lead by a Player, if so then it will add the "Battery" into the Players Backpack.
		local Backpack = Player:WaitForChild("Backpack") -- Adding "Battery" into the Players Backpack. 
		for i = 1, #ToolNames do -- Prevent spamming for the same "Battery".
			local Tool = Storage:FindFirstChild(ToolNames[i]) -- Replicating the "Battery" is in ServerStorage.
			if Tool then -- If "Battery" is in Storage
				if Backpack:FindFirstChild(Tool.Name) == nil then -- Calling the Backpack.
					game:GetService("ServerStorage").BatteryPickUp.BatteryRespawn:Fire() -- Running the "BatteryRespawn" Event from ServerStorage.
					Tool:clone().Parent = Backpack -- Cloning the "Battery" once Player uses the Tool.
					Part:Destroy() -- Destroying the "Battery" Part after when Player grabs the "Battery".
				end	
			end
		end
	end
end)

Hmm, where do you get that “player” variable you are using? Cause the error indicates that “Backpack” can’t be found.
Could you tell me what the player variable refers to?

1 Like

The EToPickUp Script is a Script that is associated into the Workspace of course.

The Error only occurs when the Player interacts with the Battery.

No, I’m talking about this line of your code:

You use a “player” variable but I can’t see any line where you define it? So, where do you define it?

1 Like

it only worked for Local (ClientSide)

You have to get the player correctly then obviously.

I have added a player variable to the Script but now I am getting this Error – 21:10:13.742 Players.BaconYTAltb.Backpack.Battery.BatteryCharge:17: attempt to index nil with 'FindFirstChild' - Server - BatteryCharge:17

Reminded back to older version

local player = game.Players:GetPlayers()
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
		getFlashlight.Handle.Battery.Value = math.clamp(getFlashlight.Handle.Battery.Value + rechargeRate, 0, maxCharge)
		tool.Handle.ClickSound:Play()
		wait (0.25)
		tool:Destroy()
		end)

But where and how do you define the player. I just don’t see it and you also won’t show me. And this code is literally the code you sent me before, I don’t need that.

I don’t know Scripting well so I am trying.

Check again I have changed.

Max char

Yeah and there is the problem. This is not how you can get the player. :GetPlayers() gets all players in the game and you can’t access a “Backpack” from there, you will have to use another method to get the player.

This should work:

tool.Activated:Connect(function()
    local player
    if (game.Players:GetPlayerFromCharacter(tool.Parent) ~= nil) then
        player = game.Players:GetPlayerFromCharacter(tool.Parent)
    end
    if player == nil then end
	local getFlashlight = player.Backpack:FindFirstChild("WeakFlashlight")
	local rechargeRate = 50
	local maxCharge = 100
	getFlashlight.Handle.Battery.Value = math.clamp(getFlashlight.Handle.Battery.Value + rechargeRate, 0, maxCharge)
	tool.Handle.ClickSound:Play()
	wait (0.25)
	tool:Destroy()
end)

I can’t test it out right now so I am not 100 percent sure but I think it should work.
And remember to remove the “local player = game.Players:GetPlayers()” at the beginning of your code or it will interfere with the tool.Activated function.

Edit: the other functions can be kept, i only changed the tool.Activated so thats why its not your whole script.

It works thank you so much! You’ve been a big help!

i think you forgot to add return

if player == nil then return end

Woops, you are absolutely right.
Seems like im a lil rusty