So I made an inspect item system and I want it so when you pick up an item that item goes into your inventory and when you click on the item you will go back to inspecting it. The only problem being is I don’t know how to detect which button was pressed assuming that if the player picks up multiple items how can I differentiate between the items in the inventory?
heres my code currently;
local num_items = 0
local inventoryItems = {}
function inventory_Item_Add(takenItem, inspectableVar)
num_items = num_items + 1
local inventoryGui = playerGui.InventoryGui
local newButton = inventoryGui.Frame.ImageButton:Clone()
newButton.Parent = inventoryGui.Main
newButton.Name = "newButtom".. num_items
if inspectableVar.Image.Value ~= nil then
newButton.Image = inspectableVar.Image.Value
else
newButton.Image = "rbxasset://textures/ui/GuiImagePlaceholder.png"
end
newButton.Visible = true
takenItem.Parent = newButton
table.insert(newButton)
end
you create a click event for that button when you pick it up and then do your other code, you can use the name of the button to see what item it is too
Well ill have to use the same click event but how can I do this because the newButton variable is the only variable that refers to a button so If I scripted the click event for it, it would only fire for the last button created.
heres an example of what im tryna say
local function CreateButton(item)
local button = Instance.new("ImageButton")
button.Image = an image
you can create a table and index them with the button name
button.Size = customizable, not need if you are using a UIGridLayout
button.MouseButton1Down:Connect(function()
DisplayItem(item)
end)
button.Parent = the gui
DisplayItem is just whatever you do to display the item, hope this helps!
But what if I pick up an item and immediantly after pickup another item wont this only work for the last item picked up?
no, it creates an event that only gets nulled if the imagebutton gets destroyed, also if they already have the item dont create the button if ti stacks
Alright ill try it out thanks for your response.