Variable's Value Set to Nil When Passed Through A Function

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am currently working on an inventory system, and am having an issue. A quantity variable that I use to defined how many stacked items there are loses its value when it is passed through a function. I would like to know how to keep the variable assigned throughout the entire script, or if I have made an error through my script.

  2. What is the issue? Include screenshots / videos if possible!
    A quantity variable defined as folder(A folder for a slot in the player, inside an inventory folder).itemQuantity (the name of the int value). Quantity is an integer value inside this folder. I am not sure why, but when it passes through a text button’s MouseButton1Up function, its value becomes nil.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have looked through multiple DevForum posts, YouTube videos, stack overflow, and other sources but cannot seem to find anything to help my situation.

-- The section where quantity is defined:
local quantity
	
for i, itemChild in pairs(slots) do
	if itemName == itemChild then
		dup = true
		folder = player:FindFirstChild("Inventory"):GetChildren()
			
		for k, child in pairs(folder) do
			if child:FindFirstChild(itemName) then
				folder = child
			end
		end
			
		local itemSlot = slotHolder:FindFirstChild(folder.Name)
		quantity = folder.itemQuantity
		quantity.Value += 1
		itemSlot:FindFirstChild("quantityLabel").Text = "x"..quantity.Value
		item.Parent = folder
        end
end


-- The section where quantity loses its value
print(quantity) -- Quantity has a value here
		
local btnPressCD = false
slot.MouseButton1Up:Connect(function()
	if btnPressCD == false then
		btnPressCD = true
				
		print(quantity) -- Quantity is nil here
				
		darkFrame.Visible = true
		drop1Btn.MouseButton1Up:Connect(function()
			dropEvent:FireServer("1", quantity, slotFolder)
			amtLabel.Text = quantity.Value
		end)
	end

I am not sure if this is caused by the fact it is a gui or something else. If any other information is needed or missing please ask so I can provide further context.

1 Like

Does quantity genuinely print nil in the output?

Near the bottom of the code, you pass quantity (which has been defined as an IntValue Instance, along with another Instance ‘slotFolder’) over to the server. iirc, you cannot directly pass instances across remotes.
(Quick note: you shouldn’t send important values such as quantity, from the client to the server. Only record inputs on the client then calculate it on the server, to prevent exploiters spoofing the values.)

You could just send quantity.Value and try to serialize the slotFolder, but in all honestly you should use a table to store the inventory.
Using instances gives almost no benefits (if any) over using a table to store the player’s inventory. They also can be very expensive (in general) and tedious to work with when storing data.

If the issue isn’t attempting to pass instances through a remote, it’s likely an underlying issue caused by another script that wasn’t provided.

Thanks for responding and thanks for the tips. I tried sending quantity.Value and it still prints nil when passed through a function. There are no other scripts, as this was simply a test and not for a game, however I did not provide the full script.

I would like to figure out why this is happening, but regardless I will take your advice and rework it to use tables. I do have a question though. If I use tables, where can I store the item when it is picked up, and how do I keep track of how many stacked items there are?

Thanks again for answering so quickly, I already see some problems with my code like bad client-server communication. Let me know if you would like to see the full script.

Sorry for responding so late, if you could provide the full script that would be great for finding out why the value is nil.
Regarding your question about how to store the quantity inside a table, you could simply store the quantity as an index in the table and give it a numerical value representing how many items they have.
If you have anymore questions don’t hesitate to let me know.