Inventory Saver isn't working

  1. What do you want to achieve? I want to make an inventory saver

  2. What is the issue? My inventory saver doesn’t work.

  3. What solutions have you tried so far? DevForum and Youtube.

I was making an Inventory system so that when players left or died they could still have their bow/wand. The code seems fine on my end could you please take a second :face_with_monocle: (look) at it please? Thanks to anyone who read this and/or helped!

local ToolFolder = game:GetService(“ServerStorage”):FindFirstChild(“SavedTools”)
local DataStoreService = game:GetService(“DataStoreService”)
local SaveData = DataStoreService:GetDataStore(“SaveData”)

game.Players.PlayerAdded:Connect(function(Player)
local ToolData = SaveData:GetAsync(Player.UserId)

local Backpack = Player:WaitForChild("Backpack")
local StarterGear = Player:WaitForChild("StarterGear")
if ToolData ~= nil then
	for i, v in pairs(ToolData)do
		if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
			ToolFolder[v]:Clone().Parent = Backpack
			ToolFolder[v]:Clone().Parent = StarterGear			
		end
	end
end
Player.CharacterRemoving:Connect(function(Character)
	Character:WaitForChild("Humanoid"):UnequipTools()
end)

end)
game.Players.PlayerRemoving:Connect(function(Player)
local ToolTable = {}
for i, v in pairs(Player.Backpack:GetChildren()) do
table.insert(ToolTable, v.Name)
end
if ToolTable ~= nil then
SaveData:SetAsync(Player.UserId, ToolTable)
end
end)

So does it not save when the player leaves the game or does the player not keep the tool once they reset? It would help if there was a more of an explanation to what the problem is.

@apoaddda My goal is to make a Inventory saver so that when the character DIES OR LEAVES THE GAME it would still give the tools that they have had previously. BTW I prefer it if you put @crazyEjai in your replies so I can answer quicker.

So this function removes the tools? (More about the CharacterRemoving function: Player | Roblox Creator Documentation on the developer hub) But basically it fires when the player dies. Could you please explain why you are using this because if a player resets/dies it automatically unequips tools from my experience.

It is supposed to do that because in my code (if done correctly) is unequipping the tools so that it goes into the backpack so that it can be cloned and put into a table which can restore the tools. Because sometimes players don’t put their tools in their backpack making it impossible to clone and store in a table.

@crazyEjai

Are you saving the tool names, then duplicating them from server storage to the player’s inventory upon joining?

1 Like

Nope, what I am doing is cloning the tools into a table then restoring them when a player dies/rejoins the game using the table.

You cannot save instances in a datastore, you will need to save the instance (Tools) Names, then duplicate them from server storage to the player’s inventory. Thats probably why your datastore is not saving the tools.

2 Likes

Gotcha, but do you have an example I can use? (I only have 2-3 weeks of coding experience so I’m a noob.)

Alright give me a sec, this will take a second.

1 Like

Try this:

local Players = game:GetService('Players');
local DatastoreService = game:GetService('DataStoreService');
local Datastore = DatastoreService:GetDataStore('SavedTools');

Players.PlayerAdded:Connect(function(plr)
	
	local suc, tools = pcall(function()
		Datastore:GetAsync(plr.UserId)
	end)
	
	if suc then
		local Tools = tools or {};
		for i,v in next, Tools do
			if game.ReplicatedStorage[v] then
				game.ReplicatedStorage[v]:Clone().Parent = plr.BackPack;
			end
		end
	end
end)

Players.PlayerRemoving:Connect(function(plr)
	local tools = {}
	for i,v in pairs(plr.BackPack:GetChildren()) do
		if v:IsA('Tool') then
			tools[#tools + 1] = v.Name;
		end
	end
	pcall(function()
		Datastore:SetAsync(plr.UserId, tools)
	end)
end)
1 Like

Keep the 1st line of script

local toolsfolder = game.ServerStorage:WaitForChild("Tools")

Change it to wait for child because the folder may not load immediately after player joins

Btw to check if the folder have the tool names, type print(tooldata)

The table gets printed, check if the printed table have the tools name

Another thing:
Print the saved table when leaving and test in studio as its easier to check the output and in game, after you leave you cannot check table

If the both the tables, after player join printed table and saved table when leaving is same

There shouldn’t be an error but for you its there, so probably you have these wrong or incorrectly saved

1 Like

It doesn’t work @LGSimulation_RBLX.

Does the thing I said works? If not send the image of the output

I gotta run @Deadwoodx and @LGSimulation_RBLX Ill get back on at 12 PM EST. Edit: and no errors in the output I RLLY gotta run cya.

1 Like

btw you cant clone tools into the startergear with local script you need one remote event.

@Deadwoodx I am back online do you have any new information that you could share?

Are you trying to clone tools into the startergear right?

1 Like

:+1: Yes I am trying to clone tools into the starter gear.

so imagine for clone tools you will need to do something like this (this is inside the ReplicatedStorage and is one local script)

game.ReplicatedStorage.RemoteEvent:FireServer("toolname") -- put the tool name here

You need to have one RemoteEvent on the RS

now inside the ServerScriptService you will put

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr,toolname)
  local clone = game.ReplicatedStorage:FindFirstChild(toolname):Clone()
  clone.Parent = plr.StarterGear
end)

when you die everything inside the StarterGear get cloned into the Backpack.

1 Like