Item Inventory not working

Hello devs. Currently I am making an item ui inventory using a folder named “Inventory” inside the player. The problem is, however, the tool is not saving inside the folder. I’ve tried switching up the datastore methods to no avail.

Client code:

--//Services
local PLAYERS = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

--//Variables
local player = PLAYERS.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local inventory = player:WaitForChild("Inventory")
local button = script.Parent
local save_tools = RS:WaitForChild("SAVING_EVENTS").SAVE_TOOLS
local inventory_event = RS:WaitForChild("SAVING_EVENTS").ITEM_INVENTORY

button.MouseButton1Down:Connect(function()
	if (character:FindFirstChildOfClass("Tool")) and (character:FindFirstChildOfClass("Tool").CanBeDropped == true) then
		local tool = character:FindFirstChildOfClass("Tool")
		
		inventory_event:FireServer(tool)
		
		button.Text = tool.Name
		
	elseif (inventory:FindFirstChild(button.Text)) then
		local tool = inventory:FindFirstChild(button.Text)
		
		button.Text = ""
		tool.Parent = player.Backpack
	end
end)

save_tools.OnClientEvent:Connect(function(item)
	
end)

Server code:

--//Services
local DSS = game:GetService("DataStoreService")
local TOOL_DS = DSS:GetDataStore("ToolsData")
local RS = game:GetService("ReplicatedStorage")
local PLAYERS = game:GetService("Players")

--//Variables
local plr = game:GetService("Players").PlayerAdded:Wait()
local inventory = plr:WaitForChild("Inventory")
local tools_folder = RS:WaitForChild("SAVED")
local save_tools_event = RS:WaitForChild("SAVING_EVENTS").SAVE_TOOLS
local item_inv = RS:WaitForChild("SAVING_EVENTS").ITEM_INVENTORY

--//Item Inventory OnServerEvent
function on_server_event(player, tool)
	tool.Parent = inventory
end

--//Player added
function player_added(player)
	repeat
		task.wait()
	until (player.Character)
	task.wait(0.5)
	
	local tools_saved = TOOL_DS:GetAsync(player.UserId.. "-tools") or {}

	for _, tool_saved in pairs(tools_saved) do
		if tools_folder:FindFirstChild(tool_saved) then
			tools_folder[tool_saved]:Clone().Parent = inventory
		end
	end

	player.CharacterRemoving:Connect(function(character)
		character.Humanoid:UnequipTools()
	end)
end

--//Player leaving
function player_leaving(player)
	local tools_owned = {}

	for _, tool_in_backpack in pairs(inventory:GetChildren()) do
		table.insert(tools_owned, tool_in_backpack.Name)
	end

	local success, error_msg = pcall(function()
		TOOL_DS:SetAsync(player.UserId.. "-tools", tools_owned)
	end)

	if error_msg then
		warn(error_msg)
	else
		print("Tools successfully saved!")
	end
end


--//Connecting
item_inv.OnServerEvent:Connect(on_server_event)
PLAYERS.PlayerAdded:Connect(player_added)
PLAYERS.PlayerRemoving:Connect(player_leaving)

Video showing the issue:

3 Likes
--//Services
local DSS = game:GetService("DataStoreService")
local TOOL_DS = DSS:GetDataStore("ToolsData")
local RS = game:GetService("ReplicatedStorage")
local PLAYERS = game:GetService("Players")

--//Variables
local tools_folder = RS:WaitForChild("SAVED")
local save_tools_event = RS:WaitForChild("SAVING_EVENTS").SAVE_TOOLS
local item_inv = RS:WaitForChild("SAVING_EVENTS").ITEM_INVENTORY

--//Item Inventory OnServerEvent
function on_server_event(player, tool)
	local inventory = player:WaitForChild("Inventory")
	tool.Parent = inventory
end

--//Player added
function player_added(player)
	repeat
		task.wait()
	until (player.Character)
	task.wait(0.5)

	local inventory = player:WaitForChild("Inventory")
	local tools_saved = TOOL_DS:GetAsync(player.UserId.. "-tools") or {}

	for _, tool in pairs(tools_saved) do
		if tools_folder:FindFirstChild(tool) then
			tools_folder[tool]:Clone().Parent = inventory
		end
	end

	player.CharacterRemoving:Connect(function(character)
		character.Humanoid:UnequipTools()
	end)
end

--//Player leaving
function player_leaving(player)
	local inventory = player:WaitForChild("Inventory")
	local tools_owned = {}

	for _, tool_in_backpack in pairs(inventory:GetChildren()) do
		table.insert(tools_owned, tool_in_backpack.Name)
	end

	local success, error_msg = pcall(function()
		TOOL_DS:SetAsync(player.UserId.. "-tools", tools_owned)
	end)

	if error_msg then
		warn(error_msg)
	else
		print("Tools successfully saved!")
	end
end


--//Connecting
item_inv.OnServerEvent:Connect(on_server_event)
PLAYERS.PlayerAdded:Connect(player_added)
PLAYERS.PlayerRemoving:Connect(player_leaving)

this might fix it

2 Likes

Its still not working. Perhaps its because the folder is created like this?

--//Services
local PLAYERS = game:GetService("Players")

PLAYERS.PlayerAdded:Connect(function(player)
	local inventory = Instance.new("Folder", player)
	inventory.Name = "Inventory"
end)
2 Likes

Bumping because I’ve still yet to find a solution

1 Like

the data stores are fine but you don’t do anything when a tool is added to the inventory, also having multiple scripts with the same source is hard to work with
image

This is much better
image

--//Services
local DSS = game:GetService("DataStoreService")
local TOOL_DS = DSS:GetDataStore("ToolsData")
local RS = game:GetService("ReplicatedStorage")
local PLAYERS = game:GetService("Players")

--//Variables
local tools_folder = RS:WaitForChild("SAVED")
local save_tools_event = RS:WaitForChild("SAVING_EVENTS").SAVE_TOOLS
local item_inv = RS:WaitForChild("SAVING_EVENTS").ITEM_INVENTORY

--//Item Inventory OnServerEvent
function on_server_event(player, tool)
	local inventory = player.Inventory
	tool.Parent = inventory
end

--//Player added
function player_added(player)
	local inventory = Instance.new("Folder", player)
	local tools_saved = TOOL_DS:GetAsync(player.UserId.. "-tools") or {}

	for _, tool_saved in pairs(tools_saved) do
		if tools_folder:FindFirstChild(tool_saved) then
			tools_folder[tool_saved]:Clone().Parent = inventory
		end
	end

	player.CharacterRemoving:Connect(function(character)
		character.Humanoid:UnequipTools()
	end)
	
	inventory.Name = "Inventory"
	inventory.Parent = player
end

--//Player leaving
function player_leaving(player)
	local tools_owned = {}
	local inventory = player.Inventory

	for _, tool_in_backpack in pairs(inventory:GetChildren()) do
		table.insert(tools_owned, tool_in_backpack.Name)
	end

	local success, error_msg = pcall(function()
		TOOL_DS:SetAsync(player.UserId.. "-tools", tools_owned)
	end)

	if error_msg then
		warn(error_msg)
	else
		print("Tools successfully saved!")
	end
end


--//Connecting
item_inv.OnServerEvent:Connect(on_server_event)
PLAYERS.PlayerAdded:Connect(player_added)
PLAYERS.PlayerRemoving:Connect(player_leaving)
--//Services
local PLAYERS = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

--//Variables
local player = PLAYERS.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local inventory = player:WaitForChild("Inventory")
local buttons = script.Parent
local save_tools = RS:WaitForChild("SAVING_EVENTS").SAVE_TOOLS
local inventory_event = RS:WaitForChild("SAVING_EVENTS").ITEM_INVENTORY

for i, button: TextButton in buttons:GetChildren() do 
	if not button:IsA("TextButton") then
		continue
	end
	
	if inventory:GetChildren()[i] then
		button.Text = inventory:GetChildren()[i].Name
	end
	
	button.MouseButton1Down:Connect(function()
		if (character:FindFirstChildOfClass("Tool")) and (character:FindFirstChildOfClass("Tool").CanBeDropped == true) then
			local tool = character:FindFirstChildOfClass("Tool")

			inventory_event:FireServer(tool)

			button.Text = tool.Name

		elseif (inventory:FindFirstChild(button.Text)) then
			local tool = inventory:FindFirstChild(button.Text)

			button.Text = ""
			tool.Parent = player.Backpack
		end
	end)
end

delete this script

If it does not work

image
make sure the tool is in the folder. It is case sensitive, so if it saves as “FLASHLIGHT,” it will not show in the inventory because it must be “Flashlight.”

2 Likes

I saw the local script parents the tool client sided

you should use another remote event

elseif (inventory:FindFirstChild(button.Text)) then
	get_tool:FireServer(button.Text)
			
	button.Text = ""
end
--// tool from inventory to backpack
function get_tool_event(Player, Tool)
	local inventory = Player.Inventory
	local tool = inventory:FindFirstChild(Tool)

	tool.Parent = Player.Backpack
end
2 Likes

Ah, so most of it works. However, when I’m removing the item from the storage, I get this error:

 ServerScriptService.SAVE_TOOLS.SAVE_TOOLS_SERVER:61: attempt to index nil with 'Parent'

When I print the tool it says “nil”

1 Like

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