Help fix bug convert string to table

String:
image
First value is NameItem and second value is Quantity
Code:


Output:

I want to convert table to string and set that to Instance Value String after print in use in another scripts by assess to Instance and convert string to table again . But i got error i don’t know how to fix that help me pls :frowning: tysm

4 Likes

Your string is not a valid JSON, you should instead try using this as the value:

{"Helmet":1}
2 Likes


input like that how i can fix that ?

This should work, if it isn’t working can you show me what the “storage” table contains.

2 Likes

That all code in chest(storage):
first it doesn’t contain any thing after when user put item in chest it contains Item Name and Item Quantity to stackable.

e.g:{apple,10}

local RS = game:GetService("ReplicatedStorage")
local HS = game:GetService("HttpService")
local Chest = script.Parent
local Promx = Chest.ProximityPrompt
local Storage = {}
local String = script.Parent.Storage

Promx.Triggered:Connect(function(player)
	RS.RemoteEvent.OpenChest:FireClient(player,Chest)
end)

RS.RemoteEvent.AddChest.OnServerEvent:Connect(function(player,Index,chest,Inventory)
	if Chest == chest then
		table.insert(Storage,Inventory[Index]) -- Item
		table.insert(Storage,Inventory[Index + 1]) -- Value
		print(Storage)
		String.Value = HS:JSONEncode(Storage)
	end
end)

Hmm, what seems to be the issue though? Upon using JSONEncode it should turn into {“Helmet”:1}?

I tested it myself but it works perfectly fine:

local t = {
    ["Helmet"] = 1
}

script.Value.Value = game:GetService("HttpService"):JSONEncode(t)
print(game:GetService("HttpService"):JSONDecode(script.Value.Value))

Nevermind I saw the issue, Instead of using table.Insert you should use it like this:

Storage[Inventory][Index] = value
2 Likes

Make sure you’re encoding & decoding properly.

Encoding Example:

local HttpService = game:GetService("HttpService")

local data = { "Helmet", 1 }
local jsonString = HttpService:JSONEncode(data)
print(jsonString)

CurrentChest.Storage.Value = jsonString

Decoding Example:

local decodedData = HttpService:JSONDecode(CurrentChest.Storage.Value)
print(decodedData[1], decodedData[2])
2 Likes

All code:
-Client:

-- GetService
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local HS = game:GetService("HttpService")
-- GUI
local player = Players.LocalPlayer
local Mouse = player:GetMouse()
local GUI = player.PlayerGui
local Sample = RS.GUI.Sample
local SampleCraft = RS.GUI.SampleCraft
local List = GUI:WaitForChild("ScreenGui").InventoryFrame.ScrollingFrame
local ArmorFrame = GUI:WaitForChild("ScreenGui").InventoryFrame.ArmorFrame
local HotBar = GUI:WaitForChild("ScreenGui").HotBar
local CraftList = GUI:WaitForChild("ScreenGui").InventoryFrame.CraftingFrame.ScrollingFrame
local RequipFrame = CraftList.Parent.Requip
local SearchBar = GUI:WaitForChild("ScreenGui").InventoryFrame.FindItem.TextBox
local SearchBarCR = GUI:WaitForChild("ScreenGui").InventoryFrame.CraftingFrame.Frame.TextBox
local Crafting = require(script.Crafting)
local Decs = GUI.ScreenGui.Desc
local ChestFrame = GUI:WaitForChild("ScreenGui").InventoryFrame.ChestFrame.ScrollingFrame
--Variable
local SampleList = {}
local SelectFrame
local NameSelect = ""
local SelectTool
local CurrentChest = nil
-- Disable
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
-- Work
for Name,Table in pairs(Crafting) do
	local New = SampleCraft:Clone()
	New.Parent = CraftList
	New.Icon.Image = RS.AllItem:FindFirstChild(Name).TextureId
	New.ItemName.Text = RS.AllItem:FindFirstChild(Name).Name
	New.Name = RS.AllItem:FindFirstChild(Name).Name

	CraftList.UIPageLayout.PageEnter:Connect(function(Page)
		NameSelect = CraftList.UIPageLayout.CurrentPage.Name
		if CraftList.UIPageLayout.CurrentPage == New then
			for i,v in pairs(RequipFrame:GetChildren()) do
				if v:IsA("ImageButton") then
					v:Destroy()
				end
			end
			for	i,v in pairs(Table) do
				local NewRq = Sample:Clone()
				NewRq.Parent = RequipFrame
				NewRq.ImageItem.Image = RS.AllItem:FindFirstChild(i).TextureId
				NewRq.ItemName.Text = "X"..v
			end
		end
	end)

	RequipFrame.Parent.Craft.MouseButton1Click:Connect(function()
		if NameSelect == Name then
			RS.RemoteEvent.Craft:FireServer(Name,Table)
		end
	end)
end

SearchBarCR:GetPropertyChangedSignal("Text"):Connect(function()
	for i,v in pairs(CraftList:GetChildren()) do
		if v:IsA("ImageButton") then
			if string.find(string.lower(CraftList.UIPageLayout.CurrentPage.Name),string.lower(SearchBarCR.Text),1,true) then
				return
			end
			CraftList.UIPageLayout:Next()
		end
	end
end)

function UpadateChest(storage)
	for i,v in pairs(ChestFrame:GetChildren()) do
		if v:IsA("ImageButton") then
			v:Destroy()
		end
	end
	for i,v in pairs(storage) do
		if type(v) == "string" then
			local New = Sample:Clone()
			New.Parent = ChestFrame
			New.ImageItem.Image = RS.AllItem:FindFirstChild(v).TextureId
			New.ItemName.Text = "X"..storage[i+1]
			New.Name = v
		end
	end
end

RS.RemoteEvent.OpenChest.OnClientEvent:Connect(function(chest)
	CurrentChest = chest
end)

RS.RemoteEvent.Update.OnClientEvent:Connect(function(Inventory,index)
	if index ~= nil	 then
		local Item = SampleList[(index+1)/2]
		Update(Item,Inventory[index],Inventory[index +1])
	end
	if #HotBar:GetChildren()-3 < 5 and #List:GetChildren() >= 3 then
		for	i=1,1,5-(#HotBar:GetChildren()-3) do
			List:GetChildren()[3].LayoutOrder = #HotBar:GetChildren() - 2
			List:GetChildren()[3].Visible = true
			List:GetChildren()[3].Parent = HotBar
		end
	end
end)

RS.RemoteEvent.Create.OnClientEvent:Connect(function(Inventory,index)
	local New = Sample:Clone()
	table.insert(SampleList,New)
	if #HotBar:GetChildren()-3 < 5 then
		New.Parent = HotBar
		New.LayoutOrder = #HotBar:GetChildren() - 3
	else
		New.Parent = List
		New.LayoutOrder = #List:GetChildren() - 2
	end
	New.Name = Inventory[index]
	Update(New,Inventory[index],Inventory[index+1])

	SearchBar:GetPropertyChangedSignal("Text"):Connect(function()
		if New.Parent == List then
			New.Visible = string.find(string.lower(Inventory[index]), SearchBar.Text, 1, true) and true or false
		end
	end)

	New.MouseEnter:Connect(function()
		if List.Parent.Visible == true then
			Decs.Visible = true
			Decs.ToolTip.Text = RS.AllItem:FindFirstChild(Inventory[index]).ToolTip
			Decs.ItemName.Text = Inventory[index]
			Decs.Size = UDim2.new(0,New.AbsoluteSize.X*3,0,New.AbsoluteSize.Y)
			Decs.Position = UDim2.new(0,New.AbsolutePosition.X + New.AbsoluteSize.X,0,New.AbsolutePosition.Y)
		end
	end)
	New.MouseLeave:Connect(function()
		Decs.Visible = false
	end)
	New.MouseButton1Click:Connect(function()
		if New.Parent == HotBar then
			SelectFrame = New
			if player.Character:FindFirstChildWhichIsA("Tool") then
				if player.Character:FindFirstChildWhichIsA("Tool").Name == Inventory[index] then
					RS.RemoteEvent.UnEquip:FireServer()
					Stroke(New)
				else
					RS.RemoteEvent.Equip:FireServer(Inventory[index])
					Stroke(New)
					local SelectFrame = RS.GUI.SelectFrame:Clone()
					SelectFrame.Size = UDim2.new(1,0,1,0)
					SelectFrame.Parent = New
					SelectFrame.Name = "select"
				end
			else
				RS.RemoteEvent.Equip:FireServer(Inventory[index])
				Stroke(New)
				local HighLight = Instance.new("UIStroke")
				local SelectFrame = RS.GUI.SelectFrame:Clone()
				SelectFrame.Size = UDim2.new(1,0,1,0)
				SelectFrame.Parent = New
				SelectFrame.Name = "select"
			end
		end
	end)
	New.MouseEnter:Connect(function()
		if List.Parent.Visible == true then
			local DragFrame = RS.GUI.DragSample:Clone()
			DragFrame.Image = New.ImageItem.Image
			DragFrame.ItemName.Text = New.ItemName.Text
			local Using = false
			DragFrame.Parent = List.Parent.Parent
			DragFrame.Position = UDim2.new(0,New.AbsolutePosition.X,0,New.AbsolutePosition.Y)
			DragFrame.Size = UDim2.new(0,New.AbsoluteSize.X,0,New.AbsoluteSize.Y)

			DragFrame.UIDragDetector.DragEnd:Connect(function()
				DragFrame:Destroy()

				local MousePos = UIS:GetMouseLocation() - game:GetService("GuiService"):GetGuiInset()
				local getGUI = player:WaitForChild("PlayerGui"):GetGuiObjectsAtPosition(MousePos.X,MousePos.Y)

				if #getGUI ~= 0 then 
					local getGUI = player:WaitForChild("PlayerGui"):GetGuiObjectsAtPosition(MousePos.X,MousePos.Y)
					for i,v in pairs(getGUI) do
						if New.Parent ~= ArmorFrame.selection and New.Parent ~= ChestFrame then
							if table.find(SampleList,v) and v.Parent ~= ArmorFrame and v.Parent ~= ChestFrame and v ~= ArmorFrame and v ~= ChestFrame and (v.Parent == HotBar or v.Parent == List) then
								print("?")
								local Index = table.find(SampleList,v)
								if Index ~= nil then
									local LastLay = SampleList[table.find(SampleList,v)].LayoutOrder
									local LastParent = SampleList[table.find(SampleList,v)].Parent
									SampleList[table.find(SampleList,v)].Parent = New.Parent
									SampleList[table.find(SampleList,v)].LayoutOrder = New.LayoutOrder
									New.Parent = LastParent
									New.LayoutOrder = LastLay
									break
								end
							elseif v.Parent == ArmorFrame.selection and v:IsA("ImageButton") and RS.AllItem:FindFirstChild(Inventory[index]):GetAttribute("Type") == "Armor" and RS.ArmorPart:FindFirstChild(Inventory[index]).Setting.Type.Value == v:GetAttribute("Type") then
								for	i,n in pairs(New.Parent:GetChildren()) do
									if n:IsA("ImageButton") then
										if New.LayoutOrder < n.LayoutOrder then
											n.LayoutOrder -= 1
										end
									end
								end

								if New.Parent == HotBar then
									for i,n in pairs(List:GetChildren()) do
										if n:IsA("ImageButton") then								
											if n.LayoutOrder == 1 then
												for	i,e in pairs(List:GetChildren()) do
													if e:IsA("ImageButton") then
														if n.LayoutOrder <= e.LayoutOrder then
															e.LayoutOrder -= 1
														end
													end
												end
												n.Parent = HotBar
												n.LayoutOrder = #HotBar:GetChildren() - 4
												break
											end
										end
									end 
								end

								New.Parent = ArmorFrame.selection
								New.Position = v.Position
								New.Size = v.Size
								v.Visible = false
								RS.RemoteEvent.EquipArmor:FireServer(Inventory[index])
								RS.RemoteEvent.UnEquip:FireServer()
								break
							elseif v.Parent == ChestFrame or v == ChestFrame and CurrentChest ~= nil then -- Add Item In Chest
								RS.RemoteEvent.AddChest:FireServer(index,CurrentChest,Inventory)
								print(HS:JSONDecode(CurrentChest.Storage.Value))
							end
						elseif New.Parent == ArmorFrame.selection then -- Unequip Armor
							if v == List.Parent or List then
								New.Size = Sample.Size
								for i,v in pairs(ArmorFrame.selection:GetChildren()) do
									if v:IsA("ImageButton") then
										if v.Position == New.Position then
											v.Visible = true
										end
									end
								end
								if #HotBar:GetChildren() - 3 < 5 then
									New.Parent = HotBar
									New.LayoutOrder = #HotBar:GetChildren() - 3
								else
									New.Parent = List
									New.LayoutOrder = #List:GetChildren() - 2
								end

								RS.RemoteEvent.UnEquipArmor:FireServer(Inventory[index])
								break
							end
						elseif New.Parent == ChestFrame then -- Remove Item From Chest
							if v == List or v.Parent == List then

							end
						end
					end
				end

				return
			end)
			DragFrame.UIDragDetector.DragStart:Connect(function()
				Using = true
			end)
			New.MouseLeave:Connect(function()
				if Using == false then
					DragFrame:Destroy()
					return
				end
			end)
		end
	end)

	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.One then
			if New.Parent == HotBar and New.LayoutOrder == 1 then
				SelectFrame = New
				if player.Character:FindFirstChildWhichIsA("Tool") then
					if player.Character:FindFirstChildWhichIsA("Tool").Name == Inventory[index] then
						RS.RemoteEvent.UnEquip:FireServer()
					else
						RS.RemoteEvent.Equip:FireServer(Inventory[index])
					end
				else
					RS.RemoteEvent.Equip:FireServer(Inventory[index])
				end
			end
		end
		if input.KeyCode == Enum.KeyCode.One then
			if New.Parent == HotBar and New.LayoutOrder == 1 then
				SelectFrame = New
				if player.Character:FindFirstChildWhichIsA("Tool") then
					if player.Character:FindFirstChildWhichIsA("Tool").Name == Inventory[index] and SelectTool == 1 then
						RS.RemoteEvent.UnEquip:FireServer()
					else
						RS.RemoteEvent.Equip:FireServer(Inventory[index])
						SelectTool = 1
					end
				else
					RS.RemoteEvent.Equip:FireServer(Inventory[index])
					SelectTool = 1
				end
			end
		end
		if input.KeyCode == Enum.KeyCode.Two then
			if New.Parent == HotBar and New.LayoutOrder == 2 then
				SelectFrame = New
				if player.Character:FindFirstChildWhichIsA("Tool") then
					if player.Character:FindFirstChildWhichIsA("Tool").Name == Inventory[index] and SelectTool == 2 then
						RS.RemoteEvent.UnEquip:FireServer()
					else
						RS.RemoteEvent.Equip:FireServer(Inventory[index])
						SelectTool = 2
					end
				else
					RS.RemoteEvent.Equip:FireServer(Inventory[index])
					SelectTool = 2
				end
			end
		end
		if input.KeyCode == Enum.KeyCode.Three then
			if New.Parent == HotBar and New.LayoutOrder == 3 then
				SelectFrame = New
				if player.Character:FindFirstChildWhichIsA("Tool") then
					if player.Character:FindFirstChildWhichIsA("Tool").Name == Inventory[index] and SelectTool == 3 then
						RS.RemoteEvent.UnEquip:FireServer()
					else
						RS.RemoteEvent.Equip:FireServer(Inventory[index])
						SelectTool = 3
					end
				else
					RS.RemoteEvent.Equip:FireServer(Inventory[index])
					SelectTool = 3
				end
			end
		end
		if input.KeyCode == Enum.KeyCode.Four then
			if New.Parent == HotBar and New.LayoutOrder == 4 then
				SelectFrame = New
				if player.Character:FindFirstChildWhichIsA("Tool") then
					if player.Character:FindFirstChildWhichIsA("Tool").Name == Inventory[index] and SelectTool == 4 then
						RS.RemoteEvent.UnEquip:FireServer()
					else
						RS.RemoteEvent.Equip:FireServer(Inventory[index])
						SelectTool = 4
					end
				else
					RS.RemoteEvent.Equip:FireServer(Inventory[index])
					SelectTool = 4
				end
			end
		end
		if input.KeyCode == Enum.KeyCode.Five then
			if New.Parent == HotBar and New.LayoutOrder == 5 then
				SelectFrame = New
				if player.Character:FindFirstChildWhichIsA("Tool") then
					if player.Character:FindFirstChildWhichIsA("Tool").Name == Inventory[index] and SelectTool == 5 then
						RS.RemoteEvent.UnEquip:FireServer()
					else
						RS.RemoteEvent.Equip:FireServer(Inventory[index])
						SelectTool = 5
					end
				else
					RS.RemoteEvent.Equip:FireServer(Inventory[index])
					SelectTool = 5
				end
			end
		end
	end)

	player.Character:FindFirstChildWhichIsA("Humanoid").Died:Connect(function()
		if New.Parent == ArmorFrame then
			New.Size = RS.GUI.Sample.Size
			for i,v in pairs(ArmorFrame:GetChildren()) do
				if v:IsA("ImageButton") then
					if v.Position == New.Position then
						v.Visible = true
					end
				end
			end
			if #HotBar:GetChildren() - 3 < 5 then
				New.Parent = HotBar
				New.LayoutOrder = #HotBar:GetChildren() - 3
			else
				New.Parent = List
				New.LayoutOrder = #List:GetChildren() - 2
			end
			RS.RemoteEvent.UnEquipArmor:FireServer(Inventory[index])
		end
	end)
end)

function Update (item,Name,Value,Expect)
	if Value <= 0 then
		if item.Parent == HotBar then
			for	i,v in pairs(HotBar:GetChildren()) do
				if v:IsA("ImageButton") then
					if item.LayoutOrder < v.LayoutOrder then
						v.LayoutOrder -= 1
					end
				end
			end
		elseif item.Parent == List then
			for	i,v in pairs(List:GetChildren()) do
				if v:IsA("ImageButton") then
					if item.LayoutOrder < v.LayoutOrder then
						v.LayoutOrder -= 1
					end
				end
			end
		elseif item.Parent == ArmorFrame then
			for i,v in pairs(ArmorFrame:GetChildren()) do
				if v:IsA("ImageButton") then
					if v.Position == item.Position then
						v.Visible = true
					end
				end
			end
		end
		table.remove(SampleList,table.find(SampleList,item))
		item:Destroy()
		Decs.Visible = false
	else
		item.ItemName.Text = "x"..Value
		item.ImageItem.Image = RS.AllItem:FindFirstChild(Name).TextureId
	end
end

function Stroke (New)
	for i,v in pairs(HotBar:GetChildren()) do
		if v:IsA("ImageButton") then
			if v:FindFirstChild("select") then
				v:FindFirstChild("select"):Destroy()
			end
		end
	end
	for i,v in pairs(List:GetChildren()) do
		if v:IsA("ImageButton") then
			if v:FindFirstChild("select") then
				v:FindFirstChild("select"):Destroy()
			end
		end
	end
	for i,v in pairs(ArmorFrame:GetChildren()) do
		if v:IsA("ImageButton") then
			if v:FindFirstChild("select") then
				v:FindFirstChild("select"):Destroy()
			end
		end
	end
end

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q and player.Character:FindFirstChildOfClass("Tool") then
		local Index = table.find(SampleList,SelectFrame)
		RS.RemoteEvent.Drop:FireServer(Index)
	end
end)

-Server

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local Stacks = require(script.Stack)
local UIS = game:GetService("UserInputService")
local DSS = game:GetService("DataStoreService")

Players.PlayerAdded:Connect(function(player)
	local Inventory = {}
	-- Added Item
	player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
		local Backpack = player.Backpack
		
		Character.ChildAdded:Connect(function(child)
			NewTool(player,Inventory,child)
		end)
	end)
	-- Added Item To Chest
	RS.RemoteEvent.AddChest.OnServerEvent:Connect(function(player,Index)
		Inventory[Index + 1] = 0
		RS.RemoteEvent.Update:FireClient(player,Inventory,Index,false)
		table.remove(Inventory,Index + 1) -- Value
		table.remove(Inventory,Index) -- Item
		print(Inventory)
	end)
	-- Crafting
	RS.RemoteEvent.Craft.OnServerEvent:Connect(function(plr,Name,Table)
		if player == plr then
			local Backpack = plr.Backpack
			local Character = plr.Character
			local CraftingItem = RS.AllItem:FindFirstChild(Name)
			local Craft = true
			for Name,Quality in pairs(Table) do -- count all resource
				local Total = 0
				for i,v in pairs(Inventory) do
					if v == Name then
						Total += Inventory[i + 1]
					end
				end
				if Quality > Total then -- check resource enough to craft
					Craft = false
				end				
			end
			if Craft then
				-- Repeat until the item is craftable
				for NameResouce,Quality in pairs(Table) do
					local currentValue = Quality
					-- Repeat until the using all resource
					repeat
						local LastChildIndex
						for i,v in pairs(Inventory) do -- Find last child
							if v == NameResouce then
								LastChildIndex = i
							end
						end
						
						if Inventory[LastChildIndex + 1] >= currentValue then -- when stack > resouce
							Inventory[LastChildIndex + 1] -= currentValue
							currentValue = 0
							RS.RemoteEvent.Update:FireClient(player,Inventory,LastChildIndex,true)
							if Inventory[LastChildIndex + 1] <= 0 then
								table.remove(Inventory,LastChildIndex + 1)
								table.remove(Inventory,LastChildIndex)
							end
						elseif Inventory[LastChildIndex + 1] < currentValue then -- when stack < resouce
							local LastValue = 0
							for i,v in pairs(Inventory) do
								if NameResouce == v then
									LastValue = i
								end
							end
							currentValue -= Inventory[LastValue + 1]
							Inventory[LastValue + 1] = 0
							RS.RemoteEvent.Update:FireClient(player,Inventory,LastValue,true)
							print(Inventory[LastValue+ 1])
							table.remove(Inventory,LastValue+ 1)
							table.remove(Inventory,LastValue)
						end
					until currentValue == 0
					
					for	i = 1,Quality do -- take resource from inventory
						if Backpack:FindFirstChild(NameResouce) then
							Backpack:FindFirstChild(NameResouce):Destroy()
						elseif Character:FindFirstChildWhichIsA("Tool") then
							if Character:FindFirstChildWhichIsA("Tool").Name == NameResouce then
								Character:FindFirstChildWhichIsA("Tool"):Destroy()
							end	
						end
					end
				end
				local newTool = RS.AllItem:FindFirstChild(Name):Clone()
				NewTool(player,Inventory,newTool)
				newTool.Parent = Backpack
			else
				RS.RemoteEvent.Warn:FireClient(player,"Not enough resource !")
			end
		end
	end)
	--Drop
	RS.RemoteEvent.Drop.OnServerEvent:Connect(function(player,index)
		local Character = player.Character
		local Backpack = player.Backpack
		local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
		if index ~= nil then
			if Inventory[index*2] >= 1 then
				Inventory[index*2] -= 1
				RS.RemoteEvent.Update:FireClient(player,Inventory,index*2 -1,true)
				if Backpack:FindFirstChild(Inventory[index*2 - 1]) then
					Backpack:FindFirstChild(Inventory[index*2 - 1]):FindFirstChild("In"):Destroy()
					Backpack:FindFirstChild(Inventory[index*2 - 1]).Handle.CFrame = Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-5)
					Backpack:FindFirstChild(Inventory[index*2 - 1]).Parent = workspace

					if Inventory[index*2] <= 0 then
						Humanoid:UnequipTools()
						table.remove(Inventory,index*2)
						table.remove(Inventory,index*2 - 1)
					end

					return
				end

				if Character:FindFirstChildWhichIsA("Tool") then
					if Character:FindFirstChildWhichIsA("Tool").Name == Inventory[index*2 - 1] then
						Character:FindFirstChildWhichIsA("Tool"):FindFirstChild("In"):Destroy()
						Character:FindFirstChildWhichIsA("Tool").Handle.CFrame = Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-5)
						Character:FindFirstChildWhichIsA("Tool").Parent = workspace

						if Inventory[index*2] <= 0 then
							Humanoid:UnequipTools()
							table.remove(Inventory,index*2)
							table.remove(Inventory,index*2 - 1)
						end

						return
					end
				end
			end

		elseif Character:FindFirstChildWhichIsA("Tool") then
			local LastChild
			local Tool = Character:FindFirstChildWhichIsA("Tool")
			for	i,v in pairs(Inventory) do
				if v == Tool.Name then
					LastChild = i
				end
			end
			Inventory[LastChild+1] -= 1
			Tool.Parent = workspace
			Tool:FindFirstChild("In"):Destroy()
			RS.RemoteEvent.Update:FireClient(player,Inventory,LastChild,true)

			if Inventory[LastChild+1] <= 0 then
				table.remove(Inventory,LastChild + 1)
				table.remove(Inventory,LastChild)
			end
		end
	end)
end)

RS.RemoteEvent.EquipArmor.OnServerEvent:Connect(function(player,ArmorName)
	local Character = player.Character
	local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
	local Backpack = player.Backpack
	local ArmorPiece = RS.ArmorPart:FindFirstChild(ArmorName):Clone()
	ArmorPiece.Parent = Character
	
	if ArmorPiece.Setting.Type.Value == "Helmet" then
		Humanoid.MaxHealth += ArmorPiece.Setting.Health.Value
		Humanoid.Health += ArmorPiece.Setting.Health.Value
	elseif ArmorPiece.Setting.Type.Value == "Chestplate" then
		Humanoid.MaxHealth += ArmorPiece.Setting.Health.Value
		Humanoid.Health += ArmorPiece.Setting.Health.Value
	elseif ArmorPiece.Setting.Type.Value == "Legging" then
		Humanoid.MaxHealth += ArmorPiece.Setting.Health.Value
		Humanoid.Health += ArmorPiece.Setting.Health.Value
	elseif ArmorPiece.Setting.Type .Value== "Boots" then
		Humanoid.MaxHealth += ArmorPiece.Setting.Health.Value
		Humanoid.Health += ArmorPiece.Setting.Health.Value
	end
	
	Wear(ArmorPiece,Character)
end)

function Wear (ArmorPiece,Character)
	for i,v in pairs(ArmorPiece:GetChildren()) do
		if v:IsA("Model") then
			
			v.PrimaryPart.Position = Character:FindFirstChild(v.Name).Position
			
			if Character:FindFirstChild(v.Name).Name == "Head" then
				for i,e in pairs(v:GetChildren()) do
					if e:IsA("MeshPart") then
						e.Position = v.PrimaryPart.Position + e.Value.Value
					end
				end
			end
			
			v.PrimaryPart.CFrame = Character:FindFirstChild(v.Name).CFrame

			local weld = Instance.new("WeldConstraint")
			weld.Part0 = Character:FindFirstChild(v.Name)
			weld.Part1 = v.PrimaryPart
			weld.Parent = v
		end
	end
end

RS.RemoteEvent.UnEquipArmor.OnServerEvent:Connect(function(player,ArmorName)
	local Character = player.Character
	local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
	local Backpack = player.Backpack
	local ArmorPiece = RS.ArmorPart:FindFirstChild(ArmorName)

	if ArmorPiece.Setting.Type.Value == "Helmet" then
		Humanoid.MaxHealth -= ArmorPiece.Setting.Health.Value
		Humanoid.Health -= ArmorPiece.Setting.Health.Value
	elseif ArmorPiece.Setting.Type.Value == "Chestplate" then
		Humanoid.MaxHealth -= ArmorPiece.Setting.Health.Value
		Humanoid.Health -= ArmorPiece.Setting.Health.Value
	elseif ArmorPiece.Setting.Type.Value == "Legging" then
		Humanoid.MaxHealth -= ArmorPiece.Setting.Health.Value
		Humanoid.Health -= ArmorPiece.Setting.Health.Value
	elseif ArmorPiece.Setting.Type .Value== "Boots" then
		Humanoid.MaxHealth -= ArmorPiece.Setting.Health.Value
		Humanoid.Health -= ArmorPiece.Setting.Health.Value
	end
	
	DeletArmor(ArmorName,Character)
end)

function DeletArmor (ArmorName,Character)
	for i,v in pairs(Character:GetChildren()) do
		if v:IsA("Model") and v.Name == ArmorName then
			for i,e in pairs(Character:GetChildren()) do
				if e:IsA("MeshPart") then
					if v:FindFirstChild(e.Name) then
						v:Destroy()
					end
				end
			end
		end
	end
end

RS.RemoteEvent.Equip.OnServerEvent:Connect(function(player,ToolName)
	local Character = player.Character
	local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
	local Tool = player.Backpack:FindFirstChild(ToolName)
	Humanoid:UnequipTools()

	if Character:FindFirstChildWhichIsA("Tool") then
		if Character:FindFirstChildWhichIsA("Tool").Name == ToolName then
			return
		end
	end

	Humanoid:EquipTool(Tool)
	return
end)
RS.RemoteEvent.UnEquip.OnServerEvent:Connect(function(player)
	local Character = player.Character
	local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
	Humanoid:UnequipTools()
	return
end)

function NewTool (player,Inventory,child)
	if child:IsA("Tool") then
		if child:FindFirstChild("In") then
			return
		else
			local In = Instance.new("BoolValue")
			In.Parent = child
			In.Name = "In"

			local have = true
			local StackFull = false
			for i,v in pairs(Inventory) do
				if v == child.Name and Inventory[i+1] < Stacks[child:GetAttribute("Type")] then
					have = false
					if Inventory[i+1] < Stacks[child:GetAttribute("Type")] then
						Inventory[i+1] += 1 
						RS.RemoteEvent.Update:FireClient(player,Inventory,i,true)
						break
					end
				end 
			end
			if have then
				table.insert(Inventory, child.Name)
				table.insert(Inventory, 1)
				RS.RemoteEvent.Create:FireClient(player,Inventory,#Inventory - 1)
			end
		end
	end
end

function OnPlayerAdded(Player)
	local SavedTools = {}
	local function OnCharacterAdded(Character)
		for Index, Tool in pairs(SavedTools) do
			Tool.Parent = Player.Backpack
		end
		SavedTools = {}
		local function OnDied()
			if Character:FindFirstChildWhichIsA("Tool") then
				Character:FindFirstChildWhichIsA("Tool").Parent = Player.Backpack
			end
			for Index, Tool in pairs(Player.Backpack:GetChildren()) do
				local CopiedTool = Tool:Clone()
				table.insert(SavedTools, CopiedTool)
			end
		end
		Character.Humanoid.Died:Connect(OnDied)
	end
	Player.CharacterAdded:Connect(OnCharacterAdded)
end
for Index, Player in pairs(Players:GetChildren()) do
	OnPlayerAdded(Player)
end
Players.PlayerAdded:Connect(OnPlayerAdded)
  • Chest
local RS = game:GetService("ReplicatedStorage")
local HS = game:GetService("HttpService")
local Chest = script.Parent
local Promx = Chest.ProximityPrompt
local Storage = {}
local String = script.Parent.Storage

Promx.Triggered:Connect(function(player)
	RS.RemoteEvent.OpenChest:FireClient(player,Chest)
end)

RS.RemoteEvent.AddChest.OnServerEvent:Connect(function(player,Index,chest,Inventory)
	if Chest == chest then
		table.insert(Storage,Inventory[Index]) -- Item
		table.insert(Storage,Inventory[Index + 1]) -- Value
		print(Storage)
		String.Value = HS:JSONEncode(Storage)
	end
end)

Chest set up :
image

that all code(Inventory sys) , i don’t know what wrong :confused: tysm

Idk but i still like that :confused: what diffent about " , " and " : " ?

1 Like

“:” is used in JSON formats to define values while “,” is a seperator for tables and JSON tables.

1 Like

In what context are you talking about?

1 Like

Hmm, I see. You are using print(HS:JSONDecode(CurrentChest.Storage.Value)) as soon as the event fires. This does not guarantee that the event was received and the value was changed in time before this function ran which is right below your event. You should instead use remote functions for this which acts like normal functions and yields the thread until the function is fully complete.

1 Like

it work !!! thank you so much :heart: :heart: :heart:
that add

CurrentChest.Storage.Changed:Connect(function()
	local decodedData = HS:JSONDecode(CurrentChest.Storage.Value)
	print(decodedData)
end)

:slight_smile: