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

How to create a Tool Storage (Backpack like)?

Hi, I'm just wondering whether it is possible to grab the way a Player can place Tools into his backpack by pressing the ` key (left of 1/! key), and using the same way they can store items in their backpack, in a brick which would store them?

Somewhat a locker/storage box, where you can place Tools and later come and pick them up from there?

1. I have a worthy tool in my Backpack, I don't want to lose it in case I die 2. I place the tool into my Storage Box, 3. I die on my adventure, 4. I respawn, I lost everything I had with me, 5. I go to my house, open the Storage Box, 6. I pick up my worthy tool, because I placed it there before going on an adventure.

Not sure if I explained it well enough, if not - Please ask questions :)

2 comments

0

I am so confused as to what you are asking for.... Are you trying to make a Backpack for a Player? And it sounds like you're trying to make a function run through people pressing certain keys on the keyboard you should study KeyBindings if you wish to do that.

0

Nonono. So when you're on ROBLOX and have over 10 tools, you can Press the ~ key, and it holds the rest of your tools. I'm trying to create a brick which would have the same properties as that ~ backpack. So I could drop tools into it, take tools out of it - A storage box.

1 answer

Short answer: Yes, it's possible.

Long answer: Depending on how you actually make the interface to drop the tool into it, it could be easier or harder.

Personally, I would make a generic API that handles taking a storing the Tool, and then using the API's functions to connect the interface.

I.E.:

local toolStorage = {}
local tools = Instance.new("Folder", game.ServerStorage)
tools.Name = "Stored Tools"
local module = {}

function deepCopy(table)
	local newTab = {}
	for i, v in pairs(table) do
		if type(v) == "table" then
			newTab[i] = deepCopy(v)
		else
			newTab[i] = v
		end
	end
	return newTab
end

function module.getPlayerStorage(player)
	if not toolStorage[player] then
		return {}
	end
	return deepCopy(toolStorage[player])
end

function module.storeToolForPlayer(player, Tool)
	if not toolStorage[player] then toolStorage[player] = {} end
	table.insert(toolStorage[player], Tool)
	Tool.Parent = tools
end

function module.retrieveFromStorage(player, Tool)
	if not toolStorage[player] then error("Player does not own that Tool!") return end
	for i, v in ipairs(toolStorage[player]) do
		if v == Tool then
			table.remove(toolStorage[player], i)
			Tool.Parent = player.Backpack
			return
		end
	end
	error("Player does not own that Tool!")
end

return module

This example only works for a single storage 'container', but you could fairly easily extend it into multiple.

2 comments

0

I don't seem to understand how this code works, well I guess I won't be able to create a Storage Box for my game, due to lack of knowledge. It would be great if the ROBLOX Backpack GUI was accessible, then I could insert it into a brick my own way. It would just open on click.

0

What specifically do you not understand?

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