Passing parameters in Gui.Activated function for Inventory Script Advice

Hi!
I am currently developing an inventory system, and as part of that, I want to make it so that when a gui button is activated, the description will change to whatever the item’s description is.

Visual


The “Header” and the “Lorem Ipsum” would change to the Item’s name and description respectively.

In order to do this, I have a module script with a table in it, which stores all of the information for items.

Module Script


The main script uses the below to get the value “Amount”

ItemInfoDictionary["Item"..treasureId.."Amount"] = ItemInfoDictionary["Item"..treasureId.."Amount"] + 1 --Replace to DataStores later

I currently use the guiButton.Activated function/event to trigger when the player activates the button. Now for this to work, I call .Activated inside the function I use to create the gui button:

local function triggerDescription(item, clickCount) 
	print(item.parent)
        -- Change description and name
end


local function makeItem(treasureId, ItemImage, ItemName) 

	local item = Instance.new("ImageButton",ItemImageFrame)
	item.Name = "Item"..treasureId
	item.BorderSizePixel = 0
	item.BackgroundTransparency = 1
	item.Image = ItemImage
	item.Activated:Connect(triggerDescription, item)
	

However by using this, I cant get the Item’s name, nor can I pass through a parameter such as TreasureId. (which is the unique id given to all items)
Without this, I cant either give the function the value it needs, or give it the name, to let it get the values directly form the ModuleScript.

I am asking for advice on how to make this (the description system) work.

I have looked around for solutions, but haven’t really found any. If you can help, that would be great! Also if you need any more information, just ask.

local newItem = makeItem() --set args, in same scope as triggerDescription function

local function triggerDescription(input, clickCount) 
	print(newItem.Parent)
	print(newItem.Name)
end

local function makeItem(treasureId, ItemImage, ItemName) 
	local item = Instance.new("ImageButton")
	item.Parent = ItemImageFrame
	item.Name = "Item"..treasureId
	item.BorderSizePixel = 0
	item.BackgroundTransparency = 1
	item.Image = ItemImage
	item.Activated:Connect(triggerDescription)
	return item
end
1 Like

You could also achieve this behavior, like this:

local function makeItem(treasureId, ItemImage, ItemName) 
	local item = Instance.new("ImageButton")
	item.Parent = ItemImageFrame
	item.Name = "Item"..treasureId
	item.BorderSizePixel = 0
	item.BackgroundTransparency = 1
	item.Image = ItemImage
	item.Activated:Connect(function(input, clickCount)
		local item = item
		--do stuff with item
	end)
end
1 Like

Thanks so much for the help! It works perfectly!