Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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)
0
Do I need to add each item name in the script? StolenLimited 8 — 7y
0
lol... this is EXACTLY why I didn't just feed him code, Goulstem. AlreadyPro 153 — 7y
0
Did you even read the contents of the answer? Goulstem 8144 — 7y
0
Yeah, but... adding the item names did nothing. StolenLimited 8 — 7y
View all comments (4 more)
0
I can't help you if you don't even read my answer. Goulstem 8144 — 7y
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'" StolenLimited 8 — 7y
0
Ok that was my bad :) Forgot a parenthasi on line 19. Goulstem 8144 — 7y
0
Thank you so much! StolenLimited 8 — 7y
Ad
Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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.

Answer this question