I made a loadout system for my game, and I just need to finish a few more features for it. And one of them is the loadout saving, which I made, however, problem is: If I want to edit the loadout, the saved items get ignored by the loadout system, so, if I were fully going to edit my loadout, I’d have 2 primaries and 2 secondaries, here’s a picture to see what I mean:
Here’s my script:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local InventoryStore = DataStoreService:GetDataStore("PlayerInventory")
local playerData = {}
-- Remove any duplicate tools from player's inventory
local function removeDuplicateTools(player)
local toolNames = {}
for _, tool in pairs(player.Backpack:GetChildren()) do
if tool:IsA("Tool") then
if toolNames[tool.Name] then
tool:Destroy()
else
toolNames[tool.Name] = true
end
end
end
for _, tool in pairs(player.Character:GetChildren()) do
if tool:IsA("Tool") then
if toolNames[tool.Name] then
tool:Destroy()
else
toolNames[tool.Name] = true
end
end
end
end
-- Function to save player's inventory
local function saveInventory(player)
local userId = player.UserId
local playerInventory = {}
for _, tool in pairs(player.Backpack:GetChildren()) do
if tool:IsA("Tool") then
table.insert(playerInventory, tool.Name)
end
end
for _, tool in pairs(player.Character:GetChildren()) do
if tool:IsA("Tool") then
table.insert(playerInventory, tool.Name)
end
end
-- Save inventory to DataStore
local success, errorMessage = pcall(function()
InventoryStore:SetAsync(userId, playerInventory)
end)
if success then
print("Inventory saved for player: " .. player.Name)
else
warn("Failed to save inventory for player: " .. player.Name .. ". Error: " .. errorMessage)
end
end
-- Function to load player's inventory
local function loadInventory(player)
local userId = player.UserId
-- Load inventory from DataStore
local success, playerInventory = pcall(function()
return InventoryStore:GetAsync(userId)
end)
if success then
if playerInventory then
-- Remove any existing tools from the backpack and character
for _, tool in pairs(player.Backpack:GetChildren()) do
if tool:IsA("Tool") then
tool:Destroy()
end
end
for _, tool in pairs(player.Character:GetChildren()) do
if tool:IsA("Tool") then
tool:Destroy()
end
end
-- Add tools back, ensuring no duplicates
local addedTools = {}
for _, toolName in pairs(playerInventory) do
if not addedTools[toolName] then
local tool = game.ReplicatedStorage.Tools:FindFirstChild(toolName) or
game.ReplicatedStorage.Coins:FindFirstChild(toolName) or
game.ReplicatedStorage.Gamepass:FindFirstChild(toolName)
if tool then
local clonedTool = tool:Clone()
clonedTool.Parent = player.Backpack
addedTools[toolName] = true
else
warn("Tool not found in ReplicatedStorage: " .. toolName)
end
end
end
print("Inventory loaded for player: " .. player.Name)
playerData[player] = {loaded = true}
else
print("No inventory found for player: " .. player.Name)
end
else
warn("Failed to load inventory for player: " .. player.Name .. ". Error: " .. playerInventory)
end
end
-- Event handlers for player joining and leaving
Players.PlayerAdded:Connect(function(player)
-- Load inventory when player joins
player.CharacterAdded:Connect(function()
if not playerData[player] or not playerData[player].loaded then
loadInventory(player)
end
-- Remove duplicate tools on character added
removeDuplicateTools(player)
end)
-- Monitor backpack changes to save automatically
player.Backpack.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
saveInventory(player)
end
end)
player.Backpack.ChildRemoved:Connect(function(child)
if child:IsA("Tool") then
saveInventory(player)
end
end)
end)
Players.PlayerRemoving:Connect(function(player)
-- Save inventory when player leaves
saveInventory(player)
playerData[player] = nil
end)