Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to add an item to the backpack when clicking on something?

I have a crate, and in that crate I've created a script (not localscript) that tries to clone a piece of meat that I have in storage and put it in the backpack. Maybe this doesn't make sense since a piece of meat is not a tool, and I need to create some sort of custom inventory storage for how much meat you have? Anyway, this is the code I've tried:

-- Create a click detector in the part in order to be able to detect clicks.

local click_detector = Instance.new('ClickDetector', script.Parent)

-- Give the tool to the player when the button is clicked.
click_detector.MouseClick:connect(function(player)
  local newMeat = game.ReplicatedStorage["Meat"]:Clone()
  
  newMeat.Parent = player.Backpack
  print("Added meat")
end)

It prints "Added meat" in the debug window when I click the crate, but nothing shows in the backpack.

1 comment

0

If its not a tool, it wont show up.

1 answer

✓ Accepted answer

In order for it to appear in the player's backpack, it needs to be a tool (as DinozCreates said). Maybe you could turn it into a tool where each time the player acquires more meat, a GUI indicates how much meat they have. Also, if the player holds the meat in their hand to eat it, it would probably be best as a tool anyway.

2 comments

0

Yea, and to turn it into a tool, just put the meat into a tool and name the meat part Handle

0

OK, thanks for the answer and comments. If I don't make it a tool, then I suppose I would need to create my own backpack or fancy GUI. Too soon for that but will keep it in mind.

This discussion is preserved as it was. New answers, comments, and votes are closed.