Help with backpack system

  1. What do I want to achieve? A proper storage system

  2. What is the issue? It is going above the limit (lets say my total capacity is 10, my value goes 11)

  3. What solutions have you tried so far? I surfed through the devforum and yt, no solution is found.

	while wait() do
		if plr.leaderstats.Strength.Value >= tonumber(plr.Storage.Value) then
			if plr.Backpack:FindFirstChild(toolName) then
				plr.Backpack:WaitForChild(toolName).GetStrength.Disabled = true
				plr.leaderstats.Strength.Value = tonumber(plr.Storage.Value)
			elseif plr.Character:FindFirstChild(toolName) then
				plr.Character:WaitForChild(toolName).GetStrength.Disabled = true
				plr.leaderstats.Strength.Value = tonumber(plr.Storage.Value)
			end
		elseif plr.leaderstats.Strength.Value < tonumber(plr.Storage.Value) then
			if plr.Backpack:FindFirstChild(toolName) then
				plr.Backpack:WaitForChild(toolName).GetStrength.Disabled = false

			elseif plr.Character:FindFirstChild(toolName) then
				plr.Character:WaitForChild(toolName).GetStrength.Disabled = false
			end
		end

	end

please help me ty

2 Likes

Your use of conditions is the problem
Use .Changed Event instead of looping.

plr.leaderstats.Strength.Changed:Connect(function(value)
	if value > tonumber(plr.Storage.Value) then
		if plr.Backpack:FindFirstChild(toolName) then
			plr.Backpack:WaitForChild(toolName).GetStrength.Disabled = true
			plr.leaderstats.Strength.Value = tonumber(plr.Storage.Value)
		end
	else
		if plr.Backpack:FindFirstChild(toolName) then
			plr.Backpack:WaitForChild(toolName).GetStrength.Disabled = false
		end
	end
end)

Instead of >= use >.
Tell me if it works :slight_smile:

Hi, i applied it to my script but it doesn’t work.

Counting in programming starts from 0, rather than 1, so you’d have to make your total capacity value a 9. I’d recommend putting the intended number in comment text next to so you don’t get confused during future edits.

i have changed it to 9, it still goes above 12

Can you show the full script please?

The way you phrased it made it seem like a math issue

if plr.leaderstats.Strength.Value >= tonumber(plr.Storage.Value) then

to:

if plr.leaderstats.Strength.Value > tonumber(plr.Storage.Value) then
elseif plr.leaderstats.Strength.Value < tonumber(plr.Storage.Value) then

to

elseif plr.leaderstats.Strength.Value <= tonumber(plr.Storage.Value) then

Hello, i wrote the code in a module script so i have multiple functions and all work except for this , anyways my code:

local remotecon = {}
local plr = game.Players.LocalPlayer

local items = game.ReplicatedStorage.Shared:WaitForChild("Tools")

function remotecon.buy(player, toolName, update)
	

	local tool = items:FindFirstChild(toolName)
	if tool ~= nil then
		if update == false  then -- Update Varible
			if not player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value ~= toolName then
				local price = tool.Cost.Value
				--plr dosent have tool
				if player.leaderstats.Coins.Value >= price  then
					print("A")
					local newtoolval = Instance.new("StringValue")
					newtoolval.Name = toolName
					newtoolval.Parent = player
					player.Equipped.Value = toolName

					for i,v in pairs(player.Backpack:GetChildren()) do
						if v:IsA("Tool") then
							v:Destroy()
						end
					end
					local gear = tool:Clone()
					gear.Parent = player.Backpack
					local gear2 = tool:Clone()
					gear2.Parent = player.Ownedtools
					player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - price
					return "Yes"

				else 
					return "No"

				end
			elseif player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value ~= toolName then
				for i,v in pairs(player.Backpack:GetChildren()) do
					if v:IsA("Tool") then
						v:Destroy()
					end
				end

				local gear = tool:Clone()
				gear.Parent = player.Backpack
				player.Equipped.Value = toolName

				return "Equipped"
			elseif player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value == toolName then
				for i,v in pairs(player.Backpack:GetChildren()) do
					if v:IsA("Tool") then
						v:Destroy()
					end
				end

				local gear = tool:Clone()
				gear.Parent = player.Backpack
				player.Equipped.Value = toolName

				return "Equipped"
			end
		else
			if not player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value ~= toolName then

				return "Not Owned" -- returns Buy and Not Owned
			elseif player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value ~= toolName then

				return "Owned" -- returns Owned
			elseif player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value == toolName then

				return "Equipped" -- returns Equipped
			end
		end
	else
		return "error"
	end
	plr.leaderstats.Strength.Changed:Connect(function(value)
		if  plr.leaderstats.Strength.Value >= tonumber(plr.Storage.Value) then
			if plr.Backpack:FindFirstChild(tool) then
				plr.Backpack:WaitForChild(tool).GetStrength.Disabled = true
				plr.leaderstats.Strength.Value = tonumber(plr.Storage.Value)
			elseif plr.Character:FindFirstChild(tool) then
				plr.Character:WaitForChild(tool).GetStrength.Disabled = true
				plr.leaderstats.Strength.Value = tonumber(plr.Storage.Value)
				
			
			end
		elseif plr.leaderstats.Strength.Value <= tonumber(plr.Storage.Value) then
			plr.Backpack:WaitForChild(tool).GetStrength.Disabled = false
		end
		
		end)

	


end
return remotecon

Hello, i applied it to my code yet it doesn’t work

hello, how can i fix this? Should i use math.Clamp()?

Hello, i used math.Clamp() too , yet it doesn’t work. I also put the code in a normal script. Please help me i tried everything. Thank you.

I found a solution :