Variable not updating at end of function

I have a variable called Amount that is supposed to change throughout the script.

function Inventory:AddItem(Config)
	print(self.Data)
	warn(Config:GetAttributes())
	for AttributeName, AttributeAmount in Config:GetAttributes() do
		local Amount = AttributeAmount--THIS VARIABLE-----------------------------
		local ItemName = string.gsub(AttributeName,"_"," ")-- "An_Item" --> "An Item"
		
		local function NoItemFound()
			while true do
				local NewSlot = Inventory:NewSlot(self.Data)
				if NewSlot then
					
					if Amount > StackLimits[ItemName] then
						NewSlot[ItemName] = StackLimits[ItemName]
						Amount -= StackLimits[ItemName]
						
					else
						NewSlot[ItemName] = Amount
						break
					end
					
				else
					break--If this is reached, the inventory is full
				end
				wait()
			end
		end
		
		while true do
			local FoundItem = Inventory:FindItem(self.Data,ItemName,true)
			if FoundItem then
				local ItemLimit = StackLimits[FoundItem[ItemName]]
				
				if Amount > ItemLimit then
					local Max =  ItemLimit - FoundItem[ItemName]
					
					FoundItem[ItemName] += Max
					Amount -= Max
				else
					FoundItem[ItemName] += Amount
					break
				end
				
			else
				NoItemFound()
				break
			end
			wait()
		end
		print("Amount:",Amount)
		
		if Amount > 0 then
			Config:SetAttribute(AttributeName,Amount)
		else
			Config:RemoveAttribute(AttributeName)
		end
		
	end
	print(self.Data)
	warn(Config:GetAttributes())
end

At the end of the function, it does this check:

if Amount > 0 then
			Config:SetAttribute(AttributeName,Amount)
		else
			Config:RemoveAttribute(AttributeName)
		end

However, Amount stays the exact same, despite the mathematical operations that were made. In the specific test I did, it ran NoItemFound() for two separate items, each of which were under the stack limit. Why is amount not changing, and how can I fix it so that it will change to do the necessary check at the end?

1 Like

Where is the script located, and is this the full script?

This is not the full script. This is a function inside a module that stores all things related to the inventory. It’s inside SeverScriptService

i’m not sure what’s going on but the code could look prettier

i’d use repeat loops instead of while loops
if they’re even needed

local function NoItemFound()
  repeat
    task.wait()
    local NewSlot = Inventory:NewSlot(self.Data)

    if Amount < StackLimits[ItemName] then -- if not Item.IsStacked then ... end
        NewSlot[ItemName] = Amount 
        break 
    end
			
    NewSlot[ItemName] = StackLimits[ItemName] -- Item.MaxAmount
    Amount -= StackLimits[ItemName]
  until not NewSlot
end

i’d also use more helper methods

Item.IsStacked: boolean
Item:UpdateAmount(NewAmount: Number)

Inventory:AddItem(ItemName: string): Item? -- You have this, but it takes config (whatever that's supposed to be)
Inventory:FindFirstItem(ItemName: string): Item? -- You have this but it doesn't seem to actually return the item
Inventory:RemoveItem(ItemName: string)

what is config?

Config is a configuration instance with attributes stored in it. Its parent is the object you’re picking up. It’s where all the item data is held in every object

this is probably unrelated to the main problem but i’m just trying to understand
i’m looking at these lines

-- Add a single item to the inventory?
function Inventory:AddItem(Config)
  for AttributeName, AttributeAmount in Config:GetAttributes() do
    warn(Config:GetAttributes())

    -- Loop through the config to add multiple items to the inventory?
    local ItemName = string.gsub(AttributeName,"_"," ") -- "An_Item" --> "An Item"

    ...

    local FoundItem = Inventory:FindItem(self.Data, ItemName, true)
  end
end

if you’re adding an item to the inventory using config (item data), how come you’re looping over config as if there’s items inside of the config? or am i reading it wrong

when you print config what does it give you?

Config has multiple attributes usually. It loops over all of the attributes (which are interpreted as items) and each iteration attempts to add as much of 1 of those items as possible. warn(Config:GetAttributes()) prints the list of all the attributes. print(Config) would just print the instance.

try adding breakpoints to where the variable is changed to see if it is running and actually changing

(sorry for the long pauses between replies i’m playing l4d2 )

could the problem be ItemName?

you get to NoItemFound() when you try to find the item and i’m assuming StackLimits[ItemName] doesn’t error (compare number > nil)

-- Stack limit must have indexes with spaces "An Item", otherwise it would error
if Amount > StackLimits["An Item"] then

...

-- Does the inventory search for items with underscores? "An_Item"
local FoundItem = Inventory:FindItem(self.Data, "An Item", true)

or
NewSlot isn’t being created and it’s ending your loops

if not NewSlot then break end

try adding some prints to see if the item was found, and the slots are being created

I found the issue and it’s really stupid.

local function NoItemFound()
			while true do
				local NewSlot = Inventory:NewSlot(self.Data)
				if NewSlot then
					
					if Amount > StackLimits[ItemName] then
						NewSlot[ItemName] = StackLimits[ItemName]
						Amount -= StackLimits[ItemName]
						
					else
						NewSlot[ItemName] = Amount
						Amount = 0--------This line needed to get added
						break
					end
					
				else
					break
				end
				wait()
			end
		end

It wasn’t updating Amount in that instance

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.