How to make items spawn it bag?

Hey guys, i’m trying to figure out how to get items into this bag, so when the zombie dies it drops a bag and you can click on it. But how do i make it so i can transfer loot from the bag? I can’t seem to make it so anything is in the bag in the first place


image

1 Like

Is this close to the thing you are asking about?

yes exactly, how do i do this?

Transfer the personal tools that kill to Lighting

If the victor touches the bag, the bag’s tools are displayed on the screen

The victor chooses what tools he needs

Sorry if my writing contains errors, I do not speak English

You can use ModuleScript for this, so you can reuse it in any Script.


Create ModuleScript and name it “BagSystem”, once your done with that, put this code; make sure to put it in the ReplicatedStorage.

local module = {} -- Create a table
function module:pickup(player, bag) -- Create a new function that requires both player and bag object
	for _, item in pairs(bag.Storage:GetChildren()) do -- Get all the ObjectValue children
		(item.Value):Clone().Parent = player.Backpack -- Clone value to the player's backpack
	end
	bag:Destroy() -- Destroy the bag, since we picked it up
end
function module:drop(position, items) -- Create a new function that requires position which is vector3 and items which is a table
	local bag = script.Bag:Clone() -- Clone the bag
	local storage = Instance.new("Folder") -- Create a new folder, this is where we gonna store the item that suppose to be in the bag
	storage.Name = "Storage" -- Rename it to "Storage"
	storage.Parent = bag -- Put the folder in the bag
	for _, item in pairs(items) do -- Get all the items from the table
		local obj = Instance.new("ObjectValue") -- Create a new object value, this is where we gonna store the location of each item
		obj.Value = item -- Set the value of the object value to the item
		obj.Parent = storage -- Put the object value in the storage folder
	end
	local input = Instance.new("ProximityPrompt") -- Create new ProximityPrompt
	input.ActionText = "Pickup" -- Set the action text to "Pickup"
	input.ObjectText = "Bag" -- Set the object text to "Bag"
	input.HoldDuration = 1 -- Set the hold duration; for this example it's set to 1
	input.MaxActivationDistance = 8 -- Set the max activation distance; for this example it's set to 8
	input.Triggered:Connect(function(player) -- Once the ProximityPrompt is triggered
		input.Enabled = false -- Prevent players from using it again
		self:pickup(player, bag) -- Call the pickup function that is located in this module
	end)
	input.Parent = bag -- Put ProximityPrompt in the bag
	bag.Position = position -- Set the position of the bag to vector3 that some script has passed to us
	bag.Parent = workspace -- Put the bag in the workspace
end
return module -- Return the module so we can require it

Be aware that we gonna use ProximityPrompt for this, instead of ClickDetector, as it’s more suitable for this purpose.

Now you got the module, create a Script and name it whatever you want; place it in the ServerScriptService and put this code.

local ReplicatedStorage = game:GetService("ReplicatedStorage") -- Get ReplicatedStorage through GetService()
local BagSystem = require(ReplicatedStorage.BagSystem) -- Require our module, so we can use it
BagSystem:drop(Vector3.new(5, 5, 5), {ReplicatedStorage.Items.MoneyStack}) -- Call drop function so we spawn a bag with items and in the specified by us location

Now once you are done with that, create a Folder called “Items” and place it in the ReplicatedStorage; make sure to put all the Tools that you want to be dropped and picked up in there; also make sure to put the bag MeshPart under the BagSystem module and also don’t forget to unanchor it and make it collidable, otherwise; everything should look like this.

image

The final result should look like this.


So this is how you create a bag system. I hope you learned something today, and if you have any questions or concerns feel free to do, otherwise; happy coding :wink:.


If you can’t get it work, here is the fully working example that I made: BagSystemExample.rbxl (49.3 KB) .

1 Like

damn, i cant seem to get your example to work, i’m no good at coding seems so complex, just kind of need the foundations but your one doesnt seem to be working :frowning:

Can you try to play test and look at the example that I had attached?

I played the one you attached, doesnt seem to be working no gui comes up

Its because ProximityPrompt is a beta feature. You can go to the File > Beta Features and you can enable it.

1 Like

You any intrested in trying to make a game together or atleast a project lol

Sorry I don’t have a time for other things as I am busy with my 2 games, and the second one is under development. But I may help you in free time.

Okay no worries, where abouts would i change what the bag would drop?

BagSystem:drop(YourZombieMode.PrimaryPart.Position, {ReplicatedStorage.Items.MoneyStack})

The first argument, is where you pass the position of where the bag will drop.

nevermind i found it, how would i put multiple items to drop?

Oh I read your question wrong. The second argument does.

BagSystem:drop(YourZombieMode.PrimaryPart.Position, {item1, item2, item3})

In the table you will put the location of each item, for example: {ReplicatedStorage.Items.Gun} you can add more by adding comma and putting another location {ReplicatedStorage.Items.Gun, ReplicatedStorage.Items.Water}.

[ServerScriptService.Main:3: Expected ‘)’ (to close ‘(’ at column 15), got ]

You forgot to put ) after }.

Sweeet now i got it working, how would i integrate it into killing and zombie or something and it dropping from the corpse

Could explain more on what you mean?

image
if i was to kill him how would i make it so he would drop the bag