So currently on my server I have a basic inventory system, and im trying to add weight so I have this
[CODE]
function ply:databaseGetValue( name )
local d = self:databaseGet()
return d[name]
end
function ply:inventoryGet()
local i = self:databaseGetValue( "inventory" )
return i
end
function ply:computeWeight( ply, name, amount, tab )
local i = self:inventoryGet()
local itemName = getItems( name )
local weight = 0
local inventory = databaseGetValue("inventory") or {}
for k,v in pairs(inventory) do
local i = getItems(k)
v.itemTWeight = v.amount * i.weight
weight = v.itemTWeight + v.itemTWeight
self:databaseSetValue( "weight", weight)
end
end
function ply:databaseSetValue( name, v )
if not v then return end
if type(v) == "table" then
if name == "inventory" then
for k,b in pairs(v) do
if b.amount <= 0 then
v[k] = nil
end
end
end
end
local d = self:databaseGet()
d[name] = v
self:databaseSave()
print( "Value " .. name .. " Set" )
end
[/CODE]
The Idea is for every item in the players inventory, it computes the total weight of each item, so if I have 10 cans each waying 2 units, it would do 10 * 2 = 20 , 20 being the itemTWeight, then for every itemTWeight it adds them up to equal the total weight of the players inventory, but for some reason this isnt working, any help would be appreciated
P.S. DatabaseSetValue() Works in other instances and I think the problem is just my for loop
[CODE]
function ply:computeWeight( ply, name, amount, tab )
local i = self:inventoryGet()
local itemName = getItems( name )
local weight = 0
local inventory = databaseGetValue("inventory") or {}
for k,v in pairs(inventory) do
local i = getItems(k)
v.itemTWeight = v.amount * i.weight
weight = v.itemTWeight + v.itemTWeight --Right here. Simple logic error
weight = weight + v.itemTWeight --Should be this
self:databaseSetValue( "weight", weight)
end
end
CHANGE weight = v.itemTWeight + v.itemTWeight TO
weight = weight + v.itemTWeight
[/CODE]
You had a simple error. You were just resetting the weight value and adding the items weight to itself. Instead of weight = weight + itemweight.
Sorry, you need to Log In to post a reply to this thread.