I am trying to make a saving inventory system for my loadout system, but it doesn’t work, I get to have 2 primaries and 2 secondaries due to my saved loadout being ignored for some reason, please help!
(pls do not mind the folder names, those are named like that because I was following another tutorial to implement different buttons that showed different weapon types, I am not good at scripting.)
Help would be greatly appreciated! : D
local tools = game.ReplicatedStorage:WaitForChild("Tools")
local conf = game.ReplicatedStorage:WaitForChild("Configuration")
local currencyName = conf:WaitForChild("CurrencyName")
local canBuyMultipleTimes = conf:WaitForChild("CanBuyItemMultipleTimes")
local rsModules = game.ReplicatedStorage:WaitForChild("ModuleScripts")
local GetTools = require(rsModules:WaitForChild("GetTools"))
local remotes = game.ReplicatedStorage:WaitForChild("RemoteEvents")
local buyToolRE = remotes:WaitForChild("BuyTool")
local SelectToolFunctions = require(script.Parent:WaitForChild("SelectTool"))
local SelectTool = SelectToolFunctions.SelectTool
local GetSelectedTool = SelectToolFunctions.GetSelectedTool
-- Keep track of purchased tools separately for each folder
local purchasedTools = {
Tools = {},
Coins = {},
Gamepass = {},
}
function UnequipToolFromFolder(plr, folderName)
for _, child in pairs(plr.Backpack:GetChildren()) do
if child:IsA("Tool") and child:IsDescendantOf(tools:FindFirstChild(folderName)) and purchasedTools[folderName][child.Name] then
child:Destroy()
end
end
for _, child in pairs(plr.Character:GetChildren()) do
if child:IsA("Tool") and child:IsDescendantOf(tools:FindFirstChild(folderName)) and purchasedTools[folderName][child.Name] then
child:Destroy()
end
end
end
function BuySelectedTool()
local currentlySelectedTool = GetSelectedTool()
local toolName = currentlySelectedTool.Name
local toolPrice = currentlySelectedTool.Configuration.Price.Value
local plrTools = GetTools(game.Players.LocalPlayer)
-- Check if the player already owns the selected tool
local toolIndex = table.find(plrTools, toolName)
if toolIndex then
-- If the player owns the tool, unequip it if it was purchased
local toolInstance = game.Players.LocalPlayer.Backpack:FindFirstChild(toolName) or game.Players.LocalPlayer.Character:FindFirstChild(toolName)
local toolFolderName = currentlySelectedTool.Parent.Name
if toolInstance and purchasedTools[toolFolderName][toolName] then
toolInstance:Destroy()
purchasedTools[toolFolderName][toolName] = nil
end
-- Remove the tool from the player's inventory
table.remove(plrTools, toolIndex)
-- Refund the player for the unequipped tool
game.Players.LocalPlayer.leaderstats[currencyName.Value].Value += toolPrice
else
-- Unequip any previously equipped tool from the same folder
local toolFolderName = currentlySelectedTool.Parent.Name
UnequipToolFromFolder(game.Players.LocalPlayer, toolFolderName)
-- Proceed with the regular purchase flow
if game.Players.LocalPlayer.leaderstats[currencyName.Value].Value >= toolPrice then
-- Mark the tool as purchased for the respective folder
purchasedTools[toolFolderName][toolName] = true
buyToolRE:FireServer(toolName)
else
-- Not enough currency to purchase the tool
return
end
end
end
return BuySelectedTool