Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago

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.

0
If its not a tool, it wont show up. DinozCreates 1070 — 5y

1 answer

Log in to vote
0
Answered by
blazar04 281 Moderation Voter
5 years ago

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.

0
Yea, and to turn it into a tool, just put the meat into a tool and name the meat part Handle ScrubSadmir 200 — 5y
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. JunkSmasher 7 — 5y
Ad

Answer this question