Hello, I am making a sword game and I have been trying to make a database that saves all the tools that a player either picks up or purchases but everything that happens doesn’t work.
local DEFAULT_TOOL = "StarterSword"
--// Services
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--// DataStore
local InventoryDataStore = DataStoreService:GetDataStore("PlayerToolInventory")
--// Folder containing all pickable tools
local ToolStorage = ReplicatedStorage:WaitForChild("ToolStorage")
--// Load saved tools
local function loadInventory(player)
local tools = {}
local success, data = pcall(function()
return InventoryDataStore:GetAsync(player.UserId)
end)
-- Clean current tools
for _, item in ipairs(player.Backpack:GetChildren()) do
if item:IsA("Tool") then item:Destroy() end
end
if success and data then
for _, toolName in pairs(data) do
local toolTemplate = ToolStorage:FindFirstChild(toolName)
if toolTemplate then
local tool = toolTemplate:Clone()
tool.Parent = player.Backpack
end
end
end
end
--// Save tools to DataStore
local function saveInventory(player)
local toolNames = {}
local seen = {}
for _, tool in ipairs(player.Backpack:GetChildren()) do
if tool:IsA("Tool") and tool.Name ~= DEFAULT_TOOL and not seen[tool.Name] then
table.insert(toolNames, tool.Name)
seen[tool.Name] = true
end
end
local success, err = pcall(function()
InventoryDataStore:SetAsync(player.UserId, toolNames)
end)
if not success then
warn("❌ Error saving inventory for", player.Name, ":", err)
end
end
--// Player joined
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
-- Give starter tool on spawn (not saved)
local defaultTool = ToolStorage:FindFirstChild(DEFAULT_TOOL)
if defaultTool then
local tool = defaultTool:Clone()
tool.Parent = player.Backpack
end
end)
print("🔄 Loading tools for", player.Name, ":")
loadInventory(player)
end)
--// Player leaving
Players.PlayerRemoving:Connect(function(player)
saveInventory(player)
end)
--// Save everyone’s tools on shutdown
game:BindToClose(function()
for _, player in ipairs(Players:GetPlayers()) do
saveInventory(player)
end
end)```