StoreTools Module

Hello Everyone! So I was in need of easy tool that could store player tools
So I’ve made a small module that lets you store your tools in server storage and get them back when you need them!

Module:

local module = {}

function module:Create(Tools : Instance,Plr : Player)
	if Plr.Character then
		if Plr.Character:FindFirstChildWhichIsA("Tool") then
			Plr.Character:FindFirstChildWhichIsA("Humanoid"):UnequipTools()
		end
	end
	local Folder = Instance.new("Folder")
	Folder.Name = Plr.Name
	Folder.Parent = game:GetService("ReplicatedStorage"):FindFirstChild("PlrFolders")
	for _,v in Tools do
		v.Parent = Folder
	end
end
function module:AddTool(Tool : Instance, FolderName : string)
	local Folder = game:GetService("ReplicatedStorage"):FindFirstChild("PlrFolders"):FindFirstChild(FolderName)
	if not Folder then return warn("Could not find a folder") end
	Tool.Parent = Folder
end
function module:GiveBack(FolderName : string, Plr : Player)
	local Folder = game:GetService("ReplicatedStorage"):FindFirstChild("PlrFolders"):FindFirstChild(FolderName)
	if not Folder then return warn("Could not find a folder") end
	for _,v in Folder:GetChildren() do
		v.Parent = Plr.Backpack
	end
	Folder:Destroy()
end

return module

Example of code usage:

local Player = game.Players.LocalPlayer
local Module = require(ReplicatedStorage:FindFirstChild("ToolFolderCreate")) -- Module
Module:Create(Player.Backpack:GetChildren(),Player)
print("Tools Stored")
wait(8)
Module:GiveBack(Player.Name,Player)
print("Tools Were given back")

If you would like to make it on server side, you could do a script that detects if event was fired

Example Code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Module = require(ReplicatedStorage:FindFirstChild("ToolFolderCreate"))
ReplicatedStorage:FindFirstChild("StoreTools").OnServerEvent:Connect(function(Player,State,Tool)
	if State == "Add" and Tool then
		Module:AddTool(Tool, Player.Name)
		elseif State == "Create" then
		Module:Create(Player.Backpack:GetChildren(),Player)
		elseif State == "Claim" then
		Module:GiveBack(Player.Name,Player)
	end
end)

and then just fire the event with the state you are doing.
like so

Example Code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
ReplicatedStorage.Events:FindFirstChild("StoreTools"):FireServer("Create") -- Create Folder
ReplicatedStorage:FindFirstChild("StoreTools"):FireServer("Add", Tool) -- Add Tool to the folder
ReplicatedStorage:FindFirstChild("StoreTools"):FireServer("Claim") -- Remove Folder and give tools to player

So this script will work for even renamed humanoids!
The question you may ask is, why I post it in this category? Because that it is my first time doing a module and i would like to receive a feedback on it and how could I improve it.

No credits needed, just use it freely!

Update 1.1
Added function to store tools!
Added checking if the folder is actually there
Added check if player have tools equipped

Old Code:

local module = {}

function module:Create(Tools : Instance,Plr : Player)
	local Folder = Instance.new("Folder")
	Folder.Name = Plr.Name
	Folder.Parent = game:GetService("ReplicatedStorage"):FindFirstChild("PlrFolders")
	for _,v in Tools do
		v.Parent = Folder
	end
end

function module:GiveBack(FolderName : string, Plr : Player)
	local Folder = game:GetService("ReplicatedStorage"):FindFirstChild("PlrFolders"):FindFirstChild(FolderName)
	for _,v in Folder:GetChildren() do
		v.Parent = Plr.Backpack
	end
	Folder:Destroy()
end

return module
1 Like