Open Source Backpack Tool Saver

Hello! When @berezaa open-sourced some of his old projects, I thought of some of the struggles I have encountered in the past, and the workarounds I had created. So today, I have created an open-source and beginner-friendly version of my backpack saver. When a player respawns, any tools that they had previously will be saved and given back to them. I would also like to point out that this will not save tools when a player leaves the game. Thank you berezza for being such an inspiration to new developers and old ones alike!

Take the model here: https://www.roblox.com/library/4976558984/Tool-Saver-Kit

Here is the entire script that you can find in the model, fully equipped with comments to help beginners understand better:

if script.Parent ~= game.ServerScriptService then -- if it isnt already in ServerScriptService it will put itself there.
	script.Parent = game.ServerScriptService
end

if not game.ServerStorage:FindFirstChild("Tools") then -- if there isnt a tools folder in ServerStorage its gonna make one.
	Tools = Instance.new("Folder")
	Tools.Parent = game.ServerStorage
	Tools.Name = "Tools"
end

function loadTools() 
	--[[ --<< REMOVE THESE IF YOU ADD MORE!
	for i,v in pairs (EXAMPLE:GetDescendants()) do 
		if v.ClassName == "Tool" then
			local clone = v:Clone()
			clone.Parent = game.ServerStorage.Tools
		end
	end
	--]] --<< REMOVE THESE IF YOU ADD MORE!
	for i,v in pairs (workspace:GetDescendants()) do -- searches for Tools in the workspace
		if v.ClassName == "Tool" then
			local clone = v:Clone()
			clone.Parent = game.ServerStorage.Tools
		end
	end
	for i,v in pairs (game.ReplicatedStorage:GetDescendants()) do -- searches for Tools in ReplicatedStorage
		if v.ClassName == "Tool" then
			local clone = v:Clone()
			clone.Parent = game.ServerStorage.Tools
		end
	end
	for i,v in pairs (game.ServerStorage:GetDescendants()) do -- searches for Tools in ServerStorage
		if v.ClassName == "Tool" and v.Parent ~= game.ServerStorage.Tools then
			local clone = v:Clone()
			clone.Parent = game.ServerStorage.Tools
		end
	end
	for i,v in pairs (game.Lighting:GetDescendants()) do -- searches for Tools in Lighting
		if v.ClassName == "Tool" then
			local clone = v:Clone()
			clone.Parent = game.ServerStorage.Tools
		end
	end

	

end
loadTools() -- fires the loadtools function






game.Players.PlayerAdded:Connect(function(player) -- this can be left alone, unless you are trying to experiment and learn :)
	local tools_table = { -- a dictionary, lookup on the wiki
	}
	player.CharacterAdded:Connect(function(character)
		for i,v in pairs (tools_table) do 
			-- when a player is added, it is checking the dictionary and loading in the items after death.
			-- print(i,v)
			if game.ServerStorage.Tools:FindFirstChild(i) then
				local clone = game.ServerStorage.Tools[i]:Clone()
				clone.Parent = player.Backpack
			end
		end
		character:FindFirstChild("Humanoid").Died:Connect(function() 
			-- print(player,"died.")
			-- when a player dies, it is going to add the names of the items they have in Backpack to the dictionary.
			for _,tool in pairs (player.Backpack:GetChildren()) do
				if not tools_table[tool.Name] then
					tools_table[tool.Name] = true
				end
				
			end
			
			for i,v in pairs (tools_table) do -- <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< EXCLUDE CERTAIN ITEMS!
				-- this excludes certain items.
				if i == "EXAMPLE" or i == "EXAMPLE2" or i == "EXAMPLE3" then -- change EXAMPLE, and add more if you need to exclude more
					--print("removed:",i)
					tools_table[i] = nil
				end
 			end

			for i,v in pairs (tools_table) do 
				-- this removes an item from the dictionary if they dropped it to prevent duplication.
				if not player.Backpack:FindFirstChild(i) then
					--print("removed:",i)
					tools_table[i] = nil
				end
 			end

			for _,tool in pairs (character:GetChildren()) do
				-- checks inside of the character and saves that too, just in case if they are holding an item when they die.
				if tool.ClassName == "Tool" then
					if not tools_table[tool.Name] then
						tools_table[tool.Name] = true
					end
			
				end
			end
		end)
	end)
end)

Additional setup instructions will also be provided in the model.

Well, this is all. I hope that some of my previous struggles can help new developers avoid this problem in the first place. Enjoy!

22 Likes

I bookmarked this awhile go. Does this also keep tools in the order players put them in them?

1 Like

Probably not since the order of the next function is arbitrary, simply replace some pairs with ipairs to retain order.

2 Likes

Fantastic system but I’m not sure I understand what use case this system is for. Why not just put a tool in the player’s Backpack and StarterGear? Everything in the player’s StarterGear is automatically replicated to the player’s backpack when they respawn, and it doesn’t require additional scripting.

Edit: I didn’t realize this post was 2 years old.