I've been trying to figure out how to do this. I just want a player's gear (backpack) to autosave so the next time they play they will have those items. Any ideas?
You need to use a table and a Data Store .
Let's take an example. Our player is Bob.
Our stuff will be into the ReplicatedStorage.
When Bob leave the game, we want to save his inventory. Note that DataStore Service doesn't save Instances.
game.Players.PlayerRemoving:connect(function(player) for _, Tool in pairs (player.BackPack:GetChildren()) do -- Stuff end end)
When Bob leaves the game, he fires a function called PlayerRemoving
and he'll be called "player".
We want to know what he has in his inventory, so we use the for
function. It will get Bob's BackPack's Stuff into a table and repeat something for each things of the table we make with the :GetChildren()
function.
Now we need to* check if the thing is a Tool*, and if yes, add it to a new table so when Bob is joining the game, we can load the stuff.
game.Players.PlayerRemoving:connect(function(player) local Backpack = {} for _, Tool in pairs (player.BackPack:GetChildren()) do if Tool:IsA("Tool") then table.insert(Backpack, tostring(Tool)) end end end)
Now, we have a table called Backpack.
Each time a tool is detected, its name will be inserted into the Backpack table.
local HttpService = game:GetService("HttpService") local DataStoreService = game:GetService("DataStoreService"):GetDataStore("Backpack") game.Players.PlayerRemoving:connect(function(player) local Backpack = {} for _, Tool in pairs (player.BackPack:GetChildren()) do if Tool:IsA("Tool") then table.insert(Backpack, tostring(Tool)) end end local BackpackJSON = HttpService:EncodeJSON(Backpack) DataStoreService:SetAsync("user_"..tostring(player.UserId), BackpackJSON) end) game.Players.PlayerAdded:connect(function(player) if DataStoreService:GetAsync("user_"..tostring(player.UserId), Stuff) then for _, NewTool in pairs (HttpService:DecodeJSON(DataStoreService:GetAsync("user_"..tostring(player.UserId))) do local Tool = game.ReplicatedStorage:FindFirstChild(tostring(NewTool)) if Tool == nil then return end local ClonedTool = Tool:Clone() ClonedTool.Parent = player.BackPack print("Added "..tostring(NewTool)) end else warn(tostring(player).." doesn't have save") end end)
Here is all the script. After inserted all the tools' name into the table, we encode the table in JSON using HttpService so everybody can understand the table. Then we save the JSON table into the DataStore Service with a key. The key is "user_" + the player's id. Mine is 25776731, so it could be "user_25776731" for me.
When a player joins, we check if the player has a save, if not then it stops. If yes, then we get the table we made using :GetAsync with the key of the player. Then we decode it. If the tool's name is inside the ReplicatedStorage, then it clones the tool, and add it to the player's backpack. Each time of each tool in the table
Hope it will work for you!!!!! (Really long)
Don't forget to accept my answer if it works!