I’m working on a mining game, and I’ve already made a datastore to save the players inventory when they leave. But just recently, I noticed that the tool the player has equipped doesn’t save. Is there any way to fix this?
Datastore Script:
l
ocal DataStoreService = game:GetService("DataStoreService")
local InventoryDataStore = DataStoreService:GetDataStore("PlayerDataWithToolValues")
local ToolsFolder = "Items"
local function LoadInventory(player)
local userId = tostring(player.UserId)
local success, playerToolsData = pcall(function()
return InventoryDataStore:GetAsync(userId)
end)
if success and playerToolsData then
local toolsFolderInstance = game.ReplicatedStorage:WaitForChild(ToolsFolder)
for itemName, toolInstancesData in playerToolsData do
local toolTemplate = toolsFolderInstance:FindFirstChild(itemName)
if toolTemplate then
for _, instanceData in toolInstancesData do
local clone = toolTemplate:Clone()
if instanceData and instanceData.Values then
for valueName, savedValue in instanceData.Values do
local valueObject = clone:FindFirstChild(valueName)
if valueObject and valueObject:IsA("ValueBase") then
valueObject.Value = savedValue
else
warn("ToolsDataStore: Could not find ValueBase '" .. valueName .. "' in cloned tool '" .. itemName .. "' or it's not a ValueBase during load.")
end
end
end
clone.Parent = player.Backpack
end
else
warn("ToolsDataStore: Could not find tool template '" .. itemName .. "' in ReplicatedStorage." .. ToolsFolder .. " during load.")
end
end
else
if not success then
warn("ToolsDataStore: Failed to load inventory for player " .. player.Name .. ": " .. tostring(playerToolsData))
end
end
end
local function SaveInventory(player)
local playerToolsData = {}
local function processTool(toolInstance)
if toolInstance:IsA("Tool") then
local itemName = toolInstance.Name
if not playerToolsData[itemName] then
playerToolsData[itemName] = {}
end
local currentToolValues = {}
for _, child in toolInstance:GetChildren() do
if child:IsA("ValueBase") then
currentToolValues[child.Name] = child.Value
end
end
table.insert(playerToolsData[itemName], { Values = currentToolValues })
end
end
if player and player.Backpack then
for _, tool in player.Backpack:GetChildren() do
processTool(tool)
end
end
if player and player.Character then
for _, tool in player.Character:GetChildren() do
processTool(tool)
end
end
if #playerToolsData == 0 and not next(playerToolsData) then
end
local userId = tostring(player.UserId)
local success, errorMsg = pcall(function()
InventoryDataStore:SetAsync(userId, playerToolsData)
end)
if not success then
warn("ToolsDataStore: Failed to save inventory for player " .. player.Name .. ": " .. errorMsg)
end
end
local function SetupCharacter(player, character)
LoadInventory(player)
character.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
SaveInventory(player)
end
end)
character.ChildRemoved:Connect(function(child)
if child:IsA("Tool") then
SaveInventory(player)
end
end)
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Died:Connect(function()
SaveInventory(player)
end)
end
end
local function InitializePlayer(player)
player.CharacterAdded:Connect(function(character)
SetupCharacter(player, character)
end)
if player.Character then
SetupCharacter(player, player.Character)
end
end
local function OnPlayerRemoving(player)
SaveInventory(player)
end
game.Players.PlayerAdded:Connect(InitializePlayer)
game.Players.PlayerRemoving:Connect(OnPlayerRemoving)
for _, player in game.Players:GetPlayers() do
InitializePlayer(player)
end
All help is appreciated. Thank you!