What do I want to achieve? A proper storage system
What is the issue? It is going above the limit (lets say my total capacity is 10, my value goes 11)
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
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)
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.
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