Need help with Making Inventory Save

  1. What do you want to achieve?
    I’m trying to make a Inventory and trying to save this inventory when player leaves

  2. What is the issue?
    For some reason it don’t works, and don’t loads…

  3. What solutions have you tried so far?
    I have been searching for this problem, but there’s no answer on my question, so i’m creating a new topic to get an solution of my issue

local Players = game:GetService("Players");
local DSS = game:GetService("DataStoreService");
local RPS = game:GetService("ReplicatedStorage");
local EquippedData = DSS:GetDataStore("EquippedData");
local InventoryData = DSS:GetDataStore("InventoryData");

local Emotes = require(RPS:WaitForChild("Modules").EmotesModule);
local Tools = require(RPS:WaitForChild("Modules").ToolsModule);

Players.PlayerAdded:Connect(function(plr)
	local DataBase = Instance.new("Folder",plr);
	DataBase.Name = plr.userId;
	local Equipped = Instance.new("Folder",DataBase);
	Equipped.Name = "Equipped";
	
	local Emote = Instance.new("StringValue", Equipped);
	Emote.Name = "Emotes";
	local Weapons = Instance.new("StringValue", Equipped);
	Weapons.Name = "Weapons";
	local Toys = Instance.new("StringValue", Equipped);
	Toys.Name = "Toys";
	
	
	local Owned = Instance.new("Folder",DataBase);
	Owned.Name = "Owned";
	local Emotes = Instance.new("Folder", Owned);
	Emotes.Name = "Emotes";
	local Weapons = Instance.new("Folder", Owned);
	Weapons.Name = "Weapons";
	local Toys = Instance.new("Folder", Owned);
	Toys.Name = "Toys";
	
	local equipped;
	local owned;
	local s,r = pcall(function()
		owned = InventoryData:GetAsync(plr.userId);
		equipped = EquippedData:GetAsync(plr.userId);
	end);
	
	if s and equipped ~= nil --[[and owned ~= nil--]] then
		pcall(function()
			--[[for name, value in pairs(owned) do
				for i, folder in pairs(Owned:GetChildren()) do
					if tostring(name) == folder.Name then
						local newVal = Instance.new("StringValue", folder);
						newVal.Name = tonumber(i);
						newVal.Value = value[i];
					end;
				end;
			end;--]]
		end);
		
		for i, value in pairs(equipped) do
			Equipped[i].Value = value;
		end;
	else
		warn(r);
	end;
end);

Players.PlayerRemoving:Connect(function(plr)
	local dataBase: Folder = plr[plr.userId];
	local equipped: Folder = dataBase["Equipped"];
	local owned: Folder = dataBase["Owned"];
	
	local equippedTable, ownedTable = {}, {};
	
	for _, folder in pairs(owned:GetChildren()) do
		for index, value in pairs(folder:GetChildren()) do
			ownedTable[folder.Name] = value.Value;
		end;
	end;
	
	for _, val in pairs(equipped:GetChildren()) do
		equippedTable[val.Name] = val.Value;
	end
	
	pcall(function()
		InventoryData:SetAsync(plr.userId, ownedTable);
	end);
	
	local s,r = pcall(function()
		EquippedData:SetAsync(plr.userId, equippedTable);
	end);
	
	if s then
		print("Equip Succes: "..plr.userId);
	end
end);

Thanks for help!

Are you trying to save the tools owned, or the tool layout?
Also, shove some prints in there and see what doesn’t print.

And don’t use Instance.new(“Type”, Parent) for many sorts of optimization reasons. Set the parent AFTER you set all other properties.

Here’s why PSA: Don't use Instance.new() with parent argument

1 Like

i want to save the tools owned, and i will optimizate my code, thanks for noticing!

There it is:

local Players = game:GetService("Players");
local DSS = game:GetService("DataStoreService");
local RPS = game:GetService("ReplicatedStorage");
local EquippedData = DSS:GetDataStore("EquippedData");
local InventoryData = DSS:GetDataStore("InventoryData");

local Emotes = require(RPS:WaitForChild("Modules").EmotesModule);
local Tools = require(RPS:WaitForChild("Modules").ToolsModule);

Players.PlayerAdded:Connect(function(plr)
	local DataBase = Instance.new("Folder");
	DataBase.Name = plr.userId;
	DataBase.Parent = plr;
	
	local Equipped = Instance.new("Folder");
	Equipped.Name = "Equipped";
	Equipped.Parent = DataBase;
	
	local Emote = Instance.new("StringValue");
	Emote.Name = "Emotes";
	Emote.Parent = Equipped;
	
	local Weapons = Instance.new("StringValue");
	Weapons.Name = "Weapons";
	Weapons.Parent = Equipped;
	
	local Toys = Instance.new("StringValue");
	Toys.Name = "Toys";
	Toys.Parent = Equipped;
	
	
	local Owned = Instance.new("Folder");
	Owned.Name = "Owned";
	Owned.Parent = DataBase;
	
	local Emotes = Instance.new("Folder");
	Emotes.Name = "Emotes";
	Emotes.Parent = Owned;
	
	local Weapons = Instance.new("Folder");
	Weapons.Name = "Weapons";
	Weapons.Parent = Owned;
	
	local Toys = Instance.new("Folder");
	Toys.Name = "Toys";
	Toys.Parent = Owned;
	
	local equipped;
	local owned;
	local s,r = pcall(function()
		owned = InventoryData:GetAsync(plr.userId);
		equipped = EquippedData:GetAsync(plr.userId);
	end);
	
	if s and equipped ~= nil --[[and owned ~= nil--]] then
		pcall(function()
			--[[for name, value in pairs(owned) do
				for i, folder in pairs(Owned:GetChildren()) do
					if tostring(name) == folder.Name then
						local newVal = Instance.new("StringValue", folder);
						newVal.Name = tonumber(i);
						newVal.Value = value[i];
					end;
				end;
			end;--]]
		end);
		
		for i, value in pairs(equipped) do
			Equipped[i].Value = value;
		end;
	else
		warn(r);
	end;
end);

Players.PlayerRemoving:Connect(function(plr)
	local dataBase: Folder = plr[plr.userId];
	local equipped: Folder = dataBase["Equipped"];
	local owned: Folder = dataBase["Owned"];
	
	local equippedTable, ownedTable = {}, {};
	
	for _, folder in pairs(owned:GetChildren()) do
		for index, value in pairs(folder:GetChildren()) do
			ownedTable[folder.Name] = value.Value;
		end;
	end;
	
	for _, val in pairs(equipped:GetChildren()) do
		equippedTable[val.Name] = val.Value;
	end
	
	pcall(function()
		InventoryData:SetAsync(plr.userId, ownedTable);
	end);
	
	local s,r = pcall(function()
		EquippedData:SetAsync(plr.userId, equippedTable);
	end);
	
	if s then
		print("Equip Succes: "..plr.userId);
	end
end);

I Got it to save Equipped Items, but i can’t understand how to loop throught the Folder to other Folder and insert values to a table, there’s the code and the Image lower:

local Players = game:GetService("Players");
local DSS = game:GetService("DataStoreService");
local RPS = game:GetService("ReplicatedStorage");
local EquippedData = DSS:GetDataStore("EquippedData");
local InventoryData = DSS:GetDataStore("InventoryData");

local Emotes = require(RPS:WaitForChild("Modules").EmotesModule);
local Tools = require(RPS:WaitForChild("Modules").ToolsModule);

Players.PlayerAdded:Connect(function(plr)
	local DataBase = Instance.new("Folder");
	DataBase.Name = plr.userId;
	DataBase.Parent = plr;
	
	local Equipped = Instance.new("Folder");
	Equipped.Name = "Equipped";
	Equipped.Parent = DataBase;
	
	local Emote = Instance.new("StringValue");
	Emote.Name = "Emotes";
	Emote.Parent = Equipped;
	
	local Weapons = Instance.new("StringValue");
	Weapons.Name = "Weapons";
	Weapons.Parent = Equipped;
	
	local Toys = Instance.new("StringValue");
	Toys.Name = "Toys";
	Toys.Parent = Equipped;
	
	
	local Owned = Instance.new("Folder");
	Owned.Name = "Owned";
	Owned.Parent = DataBase;
	
	local Emotes = Instance.new("Folder");
	Emotes.Name = "Emotes";
	Emotes.Parent = Owned;
	
	local Weapons = Instance.new("Folder");
	Weapons.Name = "Weapons";
	Weapons.Parent = Owned;
	
	local Toys = Instance.new("Folder");
	Toys.Name = "Toys";
	Toys.Parent = Owned;
	
	local equipped;
	local owned;
	local s,r = pcall(function()
		owned = InventoryData:GetAsync(plr.userId);
		equipped = EquippedData:GetAsync(plr.userId);
	end);
	
	if s then
		print("DataBase: "..plr.userId);
		print(equipped);
		for name, value in pairs(equipped) do
			Equipped[name].Value = value;
		end;
		
		for name, value in pairs(owned) do
			local newVal = Instance.new("StringValue");
			newVal.Name = "Value";
			newVal.Value = value;
			newVal.Parent = Owned[name];
		end;
	else
		warn(r);
	end;
end);

Players.PlayerRemoving:Connect(function(plr)
	local dataBase: Folder = plr[plr.userId];
	local equipped: Folder = dataBase["Equipped"];
	local owned: Folder = dataBase["Owned"];
	
	local equippedTable = {};
	local ownedTable = {};
	
	--[[for _, folder in pairs(owned:GetChildren()) do
		for index, value in pairs(folder:GetChildren()) do
			ownedTable[folder.Name] = value.Value;
		end;
	end;--]]
	
	for _, val in pairs(equipped:GetChildren()) do
		equippedTable[val.Name] = val.Value;
	end
	
	local s,r = pcall(function()
		--InventoryData:SetAsync(plr.userId, ownedTable);
		EquippedData:SetAsync(plr.userId, equippedTable);
	end);
	
	if s then
		print("Equip Succes: "..plr.userId);
	else
		warn(r);
	end
end);

Screenshot_1

Thanks for help

Nvm, i got it myself, Actually thanks for noticing about optimization

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.