How can I fix this?

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:

image

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)
1 Like

Working with one list to save, load, and edit stuff in it will be easier.

1 Like

I don’t think that helped me with the error that I am facing.

1 Like

Sorry, can you clarify? You meant that you can’t edit the saved items, right? I just gave a suggestion to make one list to load the loadout in and edit it.

To clarify:

The saved items get ignored by the loadout system, so, if I were to switch my primary, instead of replacing the primary I already have from my saved inventory, it’d just simply add it and not replace the previous primary.

Sorry incase I am in the wrong here and I misundersood, English is not my first language.

So what you want is one primary limit for each player? I don’t see anything in your code that mentions replacing a primary and removing any previous primaries.

that is done by another script. I have a seperate script for the saving inventory script since if I’d intergrate it into my loadout system it caused errors.

Do I make sense? or…

Try to make a dictionary of player loadout. {Primary = tool.Name , Secondary = tool.Name…} and while you are cloning the weapons, only clone the Primary from this list. I’m not sure what could it be, but it seems the issue is around here:

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

You are just adding weapons and not modifying primary.

If you think there is another issue with the other script, put the code where you think the issue is.

1 Like

I thought of making the script take the tools exactly from the folders they are from, and then if there’s another tool added from the same folder, it deletes the saved tool from that folder and keeps the new tool from that folder.

Maybe I could give you the script of my loadout system to see what I mean incase my explanation was not good?..

You can do that or still use the dictionary or a list which won’t be hard

local Loadout = {Primary = "OldPrimary"}
Loadout["Primary"] = "NewPrimary"

Try that first and let me know how it goes.

1 Like

OK I’mma go and try it :+1: pls wait a bit

Didn’t work out : ( Error still persisted

image

When I wanted it to be like:

image

ok so

local Loadout =  {primary = "Razelli950",
secondary = "Text"}

---So then make a for loop to add the things in that list

for i, v in Loadout do
local tool = game.ReplicatedStorage.Tools:FindFirstChild(v) or
						game.ReplicatedStorage.Coins:FindFirstChild(v) or
						game.ReplicatedStorage.Gamepass:FindFirstChild(v)
tool.Parent = backpack
.
.
.

end

v is just the weapon name in that list which is “Razelli950” then “Text”

Will this work with every Weapon?

It should work as long as you know what’s the primary weapon and the secondary will be, so when the player joins next time you know which primary weapon to load.

You probably need to save what primary weapon the player had last time they played.

So I just add in a list of every primary & secondary there is in the game? Did I understand that right?

How would the game know if the pistol is primary or secondary? You need to define it somewhere such as in the weapon module if you are using one.

I am not, I reference the folders the tools are stored in

You can use folders of course, so if the tool is a descendant of a folder called primary then you know this weapon is primary.

1 Like