I have a script that gets everything in your backpack and sells it according to it’s Worth.Value. I have one assigned to every item. But on line 39, it says “attempt to perform arithmetic (add) on number and nil”. What’s wrong? I don’t put anything in the players backpack other than any item with a Worth.Value, so it’s not like they don’t have one.
repeat
wait()
until script.Parent.Name ~= "StarterPlayerScripts"
local function sellBulk(maxCount, gem, money, count)
local AdditionGem = 0
local AdditionMoney = 0
for i, v in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do
if v:FindFirstChild("IsAGem") then
AdditionGem += v.Worth.Value
print(AdditionGem)
v:Destroy()
else
AdditionMoney += v.Worth.Value
v:Destroy()
end
if #game.Players.LocalPlayer.Backpack:GetChildren() == 0 then
return maxCount, AdditionGem, AdditionMoney, count
end
count += 1
if count == maxCount then
count = 0
local children = game.Players.LocalPlayer.Backpack:GetChildren()
return maxCount, AdditionGem, AdditionMoney, count
end
end
end
local Sells = workspace:WaitForChild("Sells")
for i,v in pairs(Sells:GetDescendants()) do
if v.Name == "Hitbox" then
v.Touched:Connect(function(part)
if part.Parent.Name == script.Parent.Parent.Name and part.Name == "HumanoidRootPart" then
local money = 0
local gem = 0
local count = 0
local A = 0
repeat
local additionUnused, AdditionGem, AdditionMoney, AdditionUnused2 = sellBulk(25, gem, money, count)
print(AdditionGem)
gem += AdditionGem
money += AdditionMoney
wait(0)
until #game.Players.LocalPlayer.Backpack:GetChildren() == 0
print(gem, money)
game.ReplicatedStorage.EggHatchingRemotes.Test:FireServer(money, gem)
wait(2)
else
print(part.Name, part.Parent.Name, game.Players.LocalPlayer.Name)
end
end)
end
end
You need to move the if #game.Players.LocalPlayer.Backpack:GetChildren() == 0 then outside the loop, because if there were no children, the loop wouldn’t even run, therefore the check wouldn’t run.
Code:
repeat
task.wait()
until script.Parent.Name ~= "StarterPlayerScripts"
local function sellBulk(maxCount, gem, money, count)
local AdditionGem = 0
local AdditionMoney = 0
if #game.Players.LocalPlayer.Backpack:GetChildren() == 0 then
return maxCount, AdditionGem, AdditionMoney, count
end
for i, v in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do
if v:FindFirstChild("IsAGem") then
AdditionGem += v.Worth.Value
print(AdditionGem)
v:Destroy()
else
AdditionMoney += v.Worth.Value
v:Destroy()
end
count += 1
if count == maxCount then
count = 0
local children = game.Players.LocalPlayer.Backpack:GetChildren()
return maxCount, AdditionGem, AdditionMoney, count
end
end
end
local Sells = workspace:WaitForChild("Sells")
for i,v in pairs(Sells:GetDescendants()) do
if v.Name == "Hitbox" then
v.Touched:Connect(function(part)
if part.Parent.Name == script.Parent.Parent.Name and part.Name == "HumanoidRootPart" then
local money = 0
local gem = 0
local count = 0
local A = 0
repeat
local additionUnused, AdditionGem, AdditionMoney, AdditionUnused2 = sellBulk(25, gem, money, count)
print(AdditionGem)
gem += AdditionGem
money += AdditionMoney
wait(0)
until #game.Players.LocalPlayer.Backpack:GetChildren() == 0
print(gem, money)
game.ReplicatedStorage.EggHatchingRemotes.Test:FireServer(money, gem)
wait(2)
else
print(part.Name, part.Parent.Name, game.Players.LocalPlayer.Name)
end
end)
end
end
Prints will save you here … nil means, as you know something isn’t there.
Also try a while loop (to test) … I’m not sure here but, I think one of them does an extra cycle.
(maybe was thinking a for next loop, is like that)
This worked, I took the “if” function out, but I also put another one in before the “count += 1” just so it checks if the backpack is empty AFTER the sell bulk, and it seemed to work. Thank you much, because now my game can function without the players getting mad ;-;