I want my script to save the player’s items in their inventory whenever something is added or they reset/die or leave the game.
The issue that I am facing is that the tools do not reload/save properly when resetting or changing your character’s “StarterCharacter” I do not know why this would affect the item saving and loading. I believe it could be a result of parenting the tools to the player’s backpacks in the wrong way/time.
I have tried to find people who could fix this for me on the Talent Hub and find solutions here on the Developer Forum but none have worked consistently and can be unreliable as my friends would join to play with me one day and come back the next completely reset empty handed.
Here is the version of the script I am currently using in the game.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Stats")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local savetools = ReplicatedStorage.CurcWeapons
local tools = {}
local function cloneTool(player, toolName)
local tool = savetools:FindFirstChild(toolName)
if tool then
local toolClone2 = tool:Clone()
toolClone2.Parent = player.Backpack
end
end
local function changeTable(player)
local char = player.Character
print(char)
tools = {}
for _, v in char:GetChildren() do
if v:IsA('Tool') then
table.insert(tools, v.Name)
end
end
local backpack = player.Backpack
if backpack then
for _, v in backpack:GetChildren() do
if v:IsA('Tool') then
table.insert(tools, v.Name)
end
end
end
end
local function onPlayerDied(player)
local char = player.Character
task.wait(7)
for _, toolName in pairs(tools) do
task.wait(.2)
cloneTool(player, toolName)
end
end
game.Players.PlayerAdded:Connect(function(player)
local key = player.UserId.."-Items"
local success = nil
local savedItems = nil
local attempt = 0
repeat
local success, savedItems = pcall(function()
return DataStore:GetAsync(key)
end)
print(savedItems)
if success and savedItems then
wait(2)
for _, item in pairs(savedItems) do
cloneTool(player, item)
end
end
until success or attempt == 5
local function humDied(char)
local humanoid = char:WaitForChild('Humanoid')
humanoid = char:WaitForChild('Humanoid')
changeTable(player)
onPlayerDied(player)
end
player.Character.Humanoid.Died:Connect(function()
humDied(player.Character)
end)
player.CharacterAdded:Connect(function(char)
char.Humanoid.Died:Connect(function()
local humanoid = char:WaitForChild('Humanoid')
humanoid = char:WaitForChild('Humanoid')
changeTable(player)
onPlayerDied(player)
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local char = player.Character
if char then
local humanoid = char:FindFirstChild('Humanoid')
if humanoid then
humanoid:UnequipTools()
end
end
local savedItems = {}
for _, item in pairs(player.Backpack:GetChildren()) do
table.insert(savedItems, item.Name)
print("Saved", item.Name)
end
local key = player.UserId.."-Items"
local success, erro = nil
local attempt = 0
repeat
local success, erro = pcall(function()
DataStore:SetAsync(key, savedItems)
end)
if not success then
print(erro)
wait(3)
end
until success or attempt == 5
local success1, aaa = pcall(function()
return DataStore:GetAsync(key)
end)
end)