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

How do I save a players gears/inventory when they leave?

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!

2 answers

✓ Accepted answer

Problem

Unfortunately, DataStores cannot compile instances. Meaning you can't directly save the gear objects themselves.


Workaround

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.


Steps

  • 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.


Code

--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)

8 comments

0

Do I need to add each item name in the script?

0

lol... this is EXACTLY why I didn't just feed him code, Goulstem.

0

Did you even read the contents of the answer?

0

Yeah, but... adding the item names did nothing.

0

I can't help you if you don't even read my answer.

0

I did. I just had bad wording. i have the items in the Folder, I also have several items in the table. Tested it, nothing worked. Ingame error message was: "Workspace.Gear Saving Script:22: ')' expected to close '(' at line 6) near 'game'"

0

Ok that was my bad :) Forgot a parenthasi on line 19.

0

Thank you so much!

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.

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