Note: I need this to be able to save a lot of different items, so please no item tables.
So, I couldn't figure out how to do this. I tried setting up a system where each item has an id in a table, i couldn't do anymore. I have a lot of items in this game. A lot. So, I just need the script to save a players gear inventory when they leave, and give them the items when they join. Thanks for the help guys!
Unfortunately, DataStores cannot compile instances. Meaning you can't directly save the gear objects themselves.
But, there is a workaround. You can save a table full of the names of each gear they own. Then, upon joining, you can iterate through this table and clone the respective tools back into their Backpack.
Before anything else, place all the possible tools in a folder in ServerStorage. This makes it so they are easily accessible.
You need to setup the DataStore. This is done using the GetDataStore
function of DataStoreService.
Specify the PlayerAdded
and PlayerRemoved
events. Check and load the data when they join, and collect and save the data as they leave.
--Setup the DataStore local ds = game:GetService("DataStoreService"):GetDataStore("Tools"); local tools = game.ServerStorage.ToolStorage; --Specify all possible tools --{[Check and Load]} game.Players.PlayerAdded:connect(function(plr) --Retrieve the potential data local data = ds:GetAsync(plr.UserId); --Check if it exists if data then --If it does, loop and clone respective tools. for _,v in next,data do local tool = tools:FindFirstChild(v); if tool then tool:Clone().Parent = plr.Backpack; end end end end) --{[Collect and Save]} game.Players.PlayerRemoving:connect(function(plr) --Make a table to hold all the data local toolList = {}; --Iterate through the backpack and fill the table in for _,v in next,plr.Backpack:GetChildren() do --Make sure it's actually a tool if v:IsA("Tool") or v:IsA("HopperBin") then toolList[#toolList+1] = v.Name; end end --Save the table ds:SetAsync(plr.UserId,toolList); end)
I'm going to suggest that you store all of the items in a Folder
somewhere inside of ServerStorage
and once a player leaves save a DataStore
with their UserId
as the key, and a table of the items names once they leave. Load the table from the DataStore
you created, and Clone
the corresponding items into the player's Backpack
.