I have a GUI where you can purchase tools for currency and I want to save those tools when you leave. They get parented to the Backpack and StarterGear but all the tutorials I found are for like when you collect the tool so they haven’t worked for me. Is there somewhere I can start for this?
Hmm may I ask if your storing these tools somewhere? If so what you can do is save a key in a datastore and from there you would want to check the key whenever the player joins the game, and if he/she has it registered then you can simply give the tool to them.
I have these in ServerStorage but I can make a folder w them
Hi
I ran in the same problem, and found a solution.
What you need to do, is create a table when the player leaves, and in that table, you store all the items the player had in its backpack
Quick example:
game.Players.PlayerRemoving:Connect(function(player) --this function triggers when a player leaves. "player" is the player who left the game.
local playerbp = {} --we create a table
for i, v in ipairs(player.Backpack:GetChildren()) do --this for loop will give us all the tools in player's backpack
table.insert(playerbp, v.Name) --this inserts tool's name to the playerbp table
end
end)
With that, we now store our table which would require the DataStoreService.
local dss game:GetService("DataStoreService") --call the ds service
local toolds = dss:GetDataStore("ToolDS") --its name can be whatever you want
---Make sure you call that info above at the start of the script, so u dont GetDataStore every function----
Now that we have the DataStoreService ready, we just make a “SetAsync” in our PlayerRemoving function
game.Players.PlayerRemoving:Connect(function(player)
local playerbp = {} --we create a table
for i, v in ipairs(player.Backpack:GetChildren()) do --this for loop will give us all the tools in player's backpack
table.insert(playerbp, v.Name) --this inserts tool's name to the playerbp table
end
toolds:SetAsync(player.UserId, playerbp) --we save playerbp table
end)
And we are ready to go! player’s backpack info is now saved
But, we have to make it load when the player enters. We use PlayerAdded and GetAsync.
We need a tool container, basically where all tools are. I use ServerStorage
local toolcontain = game.ServerStorage --change this to your tool's location if its not ss.
--Make sure this line is also at the start of the script
game.Players.PlayerAdded:Connect(function(player)
local playerbp = toolds:GetAsync(player.UserId) or {} --we get our previous saved table
for i, v in ipairs(playerbp) do
if toolcontain:FindFirstChild(v) then --we find the matching names of table and server storage children
toolcontain[v]:Clone().Parent = player.Backpack
toolcontain[v]:Clone().Parent = player.StarterGear --we clone tools
end
end
end)
And done! Our tools get saved and loaded!
I hope this helps you!
I tried this (the tools are in a folder in server storage bc I want some server storage tools to save and some not to so this just separates them) and this is my script but it didnt work
local dss = game:GetService("DataStoreService") --call the ds service
local toolds = dss:GetDataStore("ToolDS") --its name can be whatever you want
---Make sure you call that info above at the start of the script, so u dont GetDataStore every function----
local toolcontain = game.ServerStorage.Items --change this to your tool's location if its not ss.
--Make sure this line is also at the start of the script
game.Players.PlayerRemoving:Connect(function(player)
local playerbp = {} --we create a table
for i, v in ipairs(player.Backpack:GetChildren()) do --this for loop will give us all the tools in player's backpack
table.insert(playerbp, v.Name) --this inserts tool's name to the playerbp table
end
toolds:SetAsync(player.UserId, playerbp) --we save playerbp table
game.Players.PlayerAdded:Connect(function(player)
local playerbp = toolds:GetAsync(player.UserId) or {} --we get our previous saved table
for i, v in ipairs(playerbp) do
if toolcontain:FindFirstChild(v) then --we find the matching names of table and server storage children
toolcontain[v]:Clone().Parent = player.Backpack
toolcontain[v]:Clone().Parent = player.StarterGear --we clone tools
end
end
end)
end)
It just didnt save and my backpack and starter gear were empty
I moved the end)
to the end of the removing function and it worked
Weird, everything seems fine.
Any errors in the output?
Also, forgot this, add a BindToClose at the end of the script, just to make sure the info saves when the server is closing
game:BindToClose(function()
wait(3)
end)
Edit: nvm, but add the bindtoclose anyways, will help
try this:
local dss = game:GetService(“DataStoreService”) --call the ds service
local toolds = dss:GetDataStore(“ToolDS”) --its name can be whatever you want
local toolcontain = game.ReplicatedStorage.Tools --change this to your tool’s location if its not ss.game.Players.PlayerRemoving:Connect(function(player) --this function triggers when a player leaves. “player” is the player who left the game.
local success, message = pcall(function()
local playerbp = {} --we create a table
for i, v in ipairs(player.Backpack:GetChildren()) do --this for loop will give us all the tools in player’s backpack
table.insert(playerbp, v.Name) --this inserts tool’s name to the playerbp table
endtoolds:SetAsync(player.UserId, playerbp) --we save playerbp table
end)
if not success then
warn("Failed to save player data: " … message)
end
end)game.Players.PlayerAdded:Connect(function(player)
local success, message = pcall(function()
local playerbp = toolds:GetAsync(player.UserId) or {} --we get our previous saved table
for i, v in ipairs(playerbp) do
if toolcontain:FindFirstChild(v) then --we find the matching names of table and server storage children
toolcontain[v]:Clone().Parent = player.Backpack
toolcontain[v]:Clone().Parent = player.StarterGear --we clone tools
end
end
end)if not success then
warn("Failed to load player data: " … message)
end
end)
This code works, it’s based on the previous input. I fixed a few errors. I also added error handling as it was missing. Hope this helps.