I made a system to save the player’s tools when he exits the game. They are also returned to the player when they die or reset. It also has a function to give starting items, in case the player has never played. (Put the script in “ServerScriptService”)
Any problems, please comment.
local PlayersService = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local DataStoreService = game:GetService("DataStoreService")
local GameData = DataStoreService:GetDataStore("Tools")
local Tools = ServerStorage:WaitForChild("Tools") -- Your tools folder
function StarterPack(Player) -- Starting items if the player has never played
Tools:WaitForChild("Sword"):Clone().Parent = Player:WaitForChild("Backpack")
end
PlayersService.PlayerAdded:Connect(function(Player)
local GetData = true
local LastTools = {}
Player.CharacterAdded:Connect(function(Character)
if GetData then
GetData = false
local Backpack = Player:WaitForChild("Backpack")
local sucess, Data = pcall(function()
return GameData:GetAsync(Player.UserId.."-Tools")
end)
if sucess and Data ~= nil then
print(Data)
for index, Tool in ipairs(Data) do
Tools[Tool]:Clone().Parent = Backpack
end
else
StarterPack(Player)
end
else
local Backpack = Player:WaitForChild("Backpack")
for index, Tool in pairs(LastTools) do
Tools[Tool]:Clone().Parent = Backpack
end
LastTools = {}
end
end)
Player.CharacterRemoving:Connect(function(Character)
Character.Humanoid:UnequipTools()
local Backpack = Player:WaitForChild("Backpack")
for index, Tool in pairs(Backpack:GetChildren()) do
table.insert(LastTools, Tool.Name)
end
end)
end)
PlayersService.PlayerRemoving:Connect(function(Player)
local Backpack = Player:WaitForChild("Backpack")
local ToolsData = {}
for index, Tool in pairs(Backpack:GetChildren()) do
table.insert(ToolsData, Tool.Name)
end
local sucess, erro = pcall(function()
GameData:SetAsync(Player.UserId.."-Tools", ToolsData)
end)
end)
game:BindToClose(function()
for index, Player in pairs(PlayersService:GetPlayers()) do
local Backpack = Player:WaitForChild("Backpack")
local ToolsData = {}
for index, Tool in pairs(Backpack:GetChildren()) do
table.insert(ToolsData, Tool.Name)
end
local sucess, erro = pcall(function()
GameData:SetAsync(Player.UserId.."-Tools", ToolsData)
end)
end
end)