Inventory system save slots

I am making one Inventory system. It works but I want to save for every time the player join.

I am having 2 problems.

  • The tools is cloned into the backpack so when the player dies you have to select the tool again.

  • You have to select the tool every time you join.

I tried to do one IntValue system for save.

local player = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local leaderstats = player:WaitForChild("toolLeaderstats") -- I am using IntValues for unlock the GUIs(works) but I want to use they for save the tools too.

local toolsRS = RS.Tools
local scrollFrameMelee = script.Parent.Parent.Meeles



--Loadout Values--
local loadout1 = leaderstats:WaitForChild("Loadout1")
local loadout2 = leaderstats:WaitForChild("Loadout2")
local loadout3 = leaderstats:WaitForChild("Loadout3")
--loadout buttons--
local loadoutButton1 = loadoutFrame.LoadButton1
local loadoutButton2 = loadoutFrame.LoadButton2
local loadoutButton3 = loadoutFrame.LoadButton3

--------Tool Buttons ------
local classic = scrollFrameMelee.ClassicSwordButton

local equip = script.Parent.ToolButton
local ToolNameText = script.Parent.ToolName
local ToolDescp = script.Parent.ToolDescription
local ToolImage = script.Parent.ToolImage
local selected = nil

local tool1Equipped = false

local tool1 = toolsRS.ClassicSword

equip.MouseButton1Click:Connect(function()
	if selected == "classic" then
		if tool1Equipped == false then
			tool1Equipped = true
			loadoutFrame.Visible = true
			loadoutButton1.MouseButton1Click:Connect(function()
				loadout1.Value = 1
				loadoutFrame.Visible = false
			end)
			loadoutButton2.MouseButton1Click:Connect(function()
				loadout2.Value = 1
				loadoutFrame.Visible = false
			end)
			loadoutButton3.MouseButton1Click:Connect(function()
				loadout3.Value = 1
				loadoutFrame.Visible = false
			end)
			tool1:Clone().Parent = player.Backpack
			equip.Text = "Unequip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		else
			tool1Equipped = false
			equip.Text = "Equip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			player.Backpack:FindFirstChild("ClassicSword"):Destroy()
			if loadout1.Value == 1 then
				loadout1.Value = nil
			end
			if loadout2.Value == 1 then
				loadout2.Value = nil
			end
			if loadout3.Value == 1 then
				loadout3.Value = nil
			end
		end
	end
end)

---trying to clone the tools when the player join.
game.Players.PlayerAdded:Connect(function()
----------------------------------------
	print("s")
	if loadout1.Value == 1 then
		tool1Equipped = true
		tool1:Clone().Parent = player.Backpack
	elseif loadout1.Value == 2 then
		tool2Equipped = true
		
----------------------------------------
	if loadout2.Value == 1 then
			tool1Equipped = true
			tool1:Clone().Parent = player.Backpack
			
	elseif loadout2.Value == 2 then
			tool2Equipped = true
			tool2:Clone().Parent = player.Backpack
		end
	end
end)

this is just one test I am making for see if I can save the tools.

Gif:

The values changes.

I am trying to make something like:

  • Max 3 slots for inside the backpack (I am still trying to work on it)

  • Clone tools in the backpack everytime the player join and clone everytime the character spawns. (Maybe one function I can use for it).

1 Like

For max backpack slots:

local limit = 3

for i,v in pairs(game.Players.LocalPlayer.LocalPlayer.Backpack:GetChildren()) do
 if i >= limit + 1 then
   v:Destroy()
  end
end

For tool to respawn in player backpack after death:

Clone the objects to player.StarterPack as backpack will reset after player dies, if you clone to starter pack the objects will clone to backpack automatically

For tools saving

local dss = game:GetService("DataStoreService")
local dsfortools = dss:GetDataStore("ToolsSave")

game.Players.PlayerAdded:Connect(function(plr)
 if dsfortools:GetAsync(plr.UserId) then
    local tabletoload = dsfortools:GetAsync(plr.UserId)
    for i,v in pairs(tabletoload) do
       if replicatedstorage:FindFirstChild(v) then
           local clone = replicatedstorage:FindFirstChild(v):Clone()
           clone.Parent = plr.StarterPack
        end
     end
  end
end)

game.Players.PlayerRemoving:Connect(function(plr)
  local newtable = {}
   for i,v in pairs(plr.Backpack:GetChildren()) do
     table.insert(newtable,v.Name)
  end
end)

So, you keep all your tools in replicated storage or a folder in replicated storage, you save the tool names when player is leaving and then when they join back, if they have their user id saved their table will load then, from the loaded table you clone tools and keep in starter pack for respawn for tools after player dies

wait you can just clone the tool inside the starterpack from one local script?

Yes you can, I guess but thats how it respawns after player dies, and don’t save big data stores for tools, use name system using a table and save the table

One more question for the max slots in inventory I cant just do that because the gui will change I can do something like when the backpack limit is 3 this can say “Inventory is full”

And when the player join I have the same problem.

What do you mean by gui will change?

You can do something before equipping a tool:

if #game.Players.LocalPlayer.Backpack:GetChildren() >= 3 then
   print("Limit of backpack reached")
else
 -- the function to give player tool
end

Like if I do that the gui will change but the tool will not clone.

Use a remote event for tools clone, you cannot do clone of tool using a local script, then a server side script to clone objects in their inventory

If you didn’t understand that, see this:

  • Add a remove event in ReplicatedStorage
  • A server side script in server script service, which will be the function for remote event
  • In a local script, fire the remote event to get the cloned tool

Thats how you clone the tool

I cant just do something like:

local limit = nil

classic.MouseButton1Click:Connect(function()

if limit >= 3 then
equip.Text = “Backpack is full”
end

selected = "classic"
if tool1Equipped == false then
	equip.Text = "Equip weapon" 
	equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
	ToolNameText.Text = "Tool: "..tool1.Name
	ToolDescp.Text = "Description: Classic Roblox sword"
else
	equip.Text = "Unequip weapon" 
	equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
	ToolNameText.Text = "Tool: "..tool1.Name
	ToolDescp.Text = "Description: Classic Roblox sword"
end

end)

for the max slots

You can but thats why too messed up and complicated, if you try the script I sent it worked many times for me and you can also try it, its just an 8 line script

Try to use

for i,v in pairs(buttons) do
 if v:IsA("TextButton") then
   v.MouseButton1Click:Connect(function()
     -- equip.Text = v.Name
    end)
  end
end

If I turn off the ResetOnSpawn the gui will not reset but the tool will get deleted after death so I have to use one Remote event for clone to starterpack?

Clone the objects to player.StarterPack as backpack will reset after player dies, if you clone to starter pack the objects will clone to backpack automatically

This line, if you clone to starter pack when equiping, you don’t any script to respawn it in player backpack again

I know everything inside the starterpack clones into the backpack (like starterGui clones into the PlayerGui) but I cant just make something like

tool:Clone().Parent = player.Backpack
tool:Clone().Parent = player.StarterPack – something like this?

Yes you can but make sure your not doing that script line from a local script

I can clone into the StaterGear and clone everything inside the StaterGear into the Backpack

1 Like

If anything is in StarterPack you don’t need to keep them in backpack, its like default starter pack which is in the explorer, anything there will go to backpack

I am having problems cloning the tool into the starterPack

What are the errors or problems?