✓ 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
local ds = game:GetService("DataStoreService"):GetDataStore("Tools");
local tools = game.ServerStorage.ToolStorage;
game.Players.PlayerAdded:connect(function(plr)
local data = ds:GetAsync(plr.UserId);
if data then
for _,v in next,data do
local tool = tools:FindFirstChild(v);
if tool then
tool:Clone().Parent = plr.Backpack;
end
end
end
end)
game.Players.PlayerRemoving:connect(function(plr)
local toolList = {};
for _,v in next,plr.Backpack:GetChildren() do
if v:IsA("Tool") or v:IsA("HopperBin") then
toolList[#toolList+1] = v.Name;
end
end
ds:SetAsync(plr.UserId,toolList);
end)
8 comments
Do I need to add each item name in the script?
lol... this is EXACTLY why I didn't just feed him code, Goulstem.
Did you even read the contents of the answer?
Yeah, but... adding the item names did nothing.
I can't help you if you don't even read my answer.
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'"
Ok that was my bad :) Forgot a parenthasi on line 19.
Thank you so much!