local DataStore = game:GetService("DataStoreService"):GetDataStore("InventoryKey_Version" .. game.ServerScriptService.DataStoreService.Service.Version.Value)
local SlotDataStore = game:GetService("DataStoreService"):GetDataStore("SlotKey_Version" .. game.ServerScriptService.DataStoreService.Service.Version.Value)

local HTTP = game:GetService("HttpService")
local itemModule = require(game.ReplicatedStorage.Assets.Objects.Utility.Items)

game:GetService("Players").PlayerAdded:Connect(function(Player)
	local newData = DataStore:GetAsync(Player.UserId)
	local slotData = SlotDataStore:GetAsync(Player.UserId)
	
	local Folder = Instance.new("Folder")
	Folder.Parent = game.ServerStorage.Assets.Inventory
	Folder.Name = Player.Name
	
	local toolFolder = Instance.new("Folder")
	toolFolder.Parent = Folder
	toolFolder.Name = "Tools"
	
	local Slot1 = Instance.new("Folder")
	Slot1.Parent = toolFolder
	Slot1.Name = "Slot1"
	
	local Slot2 = Instance.new("Folder")
	Slot2.Parent = toolFolder
	Slot2.Name = "Slot2"
	
	local Slot3 = Instance.new("Folder")
	Slot3.Parent = toolFolder
	Slot3.Name = "Slot3"
	
	local Slot4 = Instance.new("Folder")
	Slot4.Parent = toolFolder
	Slot4.Name = "Slot4"
	
	local Slot5 = Instance.new("Folder")
	Slot5.Parent = toolFolder
	Slot5.Name = "Slot5"
	
	local Slot6 = Instance.new("Folder")
	Slot6.Parent = toolFolder
	Slot6.Name = "Slot6"
	
	if slotData then
		slotData = HTTP:JSONDecode(slotData)
		
		wait(0.5)
		
		if slotData[game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot1:GetChildren()] then
			print(slotData)
			for i, v in pairs(game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot1:GetChildren()) do
				if v:IsA("StringValue") then
					v.Name = v.Value
				end
				for i, v2 in pairs(v:GetChildren()) do
					if v:IsA("IntValue") then
						v.Name = v.Amount.Value
					end
				end
			end
		end
		
		if slotData[game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot2:GetChildren()] then
			for i, v in pairs(game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot2:GetChildren()) do
				if v:IsA("StringValue") then
					v.Name = v.Value
				end
				for i, v2 in pairs(v:GetChildren()) do
					if v:IsA("IntValue") then
						v.Name = v.Amount.Value
					end
				end
			end
		end
		
		if slotData[game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot3:GetChildren()] then
			for i, v in pairs(game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot3:GetChildren()) do
				if v:IsA("StringValue") then
					v.Name = v.Value
				end
				for i, v2 in pairs(v:GetChildren()) do
					if v:IsA("IntValue") then
						v.Name = v.Amount.Value
					end
				end
			end
		end
		
		if slotData[game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot4:GetChildren()] then
			for i, v in pairs(game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot4:GetChildren()) do
				if v:IsA("StringValue") then
					v.Name = v.Value
				end
				for i, v2 in pairs(v:GetChildren()) do
					if v:IsA("IntValue") then
						v.Name = v.Amount.Value
					end
				end
			end
		end
		
		if slotData[game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot5:GetChildren()] then
			for i, v in pairs(game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot5:GetChildren()) do
				if v:IsA("StringValue") then
					
					v.Name = v.Value
				end
				for i, v2 in pairs(v:GetChildren()) do
					if v:IsA("IntValue") then
						v.Name = v.Amount.Value
					end
				end
			end
		end
		
		if slotData[game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot6:GetChildren()] then
			for i, v in pairs(game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot6:GetChildren()) do
				if v:IsA("StringValue") then
					v.Name = v.Value
				end
				for i, v2 in pairs(v:GetChildren()) do
					if v:IsA("IntValue") then
						v.Name = v.Amount.Value
					end
				end
			end
		end
	end
	
	if newData then
		newData = HTTP:JSONDecode(newData)
		wait(0.5)
		
		if newData.Apple ~= nil then
			if newData.Apple > 0 then
				local Apple = Instance.new("StringValue")
				Apple.Parent = Folder
				Apple.Name = "Apple"
				local Amount = Instance.new("IntValue")
				Amount.Parent = Apple
				Amount.Name = "Amount"
				Amount.Value = newData.Apple
			end
		end
		
		if newData["Magic Potion"] ~= nil then
			if newData["Magic Potion"] > 0 then
				local MP = Instance.new("StringValue")
				MP.Parent = Folder
				MP.Name = "Magic Potion"
				local Amount = Instance.new("IntValue")
				Amount.Parent = MP
				Amount.Name = "Amount"
				Amount.Value = newData["Magic Potion"]
			end
		end
		
		if newData["Lesser Healing Potion"] ~= nil then
			if newData["Lesser Healing Potion"] > 0 then
				local LHP = Instance.new("StringValue")
				LHP.Parent = Folder
				LHP.Name = "Lesser Healing Potion"
				local Amount = Instance.new("IntValue")
				Amount.Parent = LHP
				Amount.Name = "Amount"
				Amount.Value = newData["Lesser Healing Potion"]
			end
		end
		
		if newData["Healing Potion"] ~= nil then
			if newData["Healing Potion"] > 0 then
				local HP = Instance.new("StringValue")
				HP.Parent = Folder
				HP.Name = "Lesser Healing Potion"
				local Amount = Instance.new("IntValue")
				Amount.Parent = HP
				Amount.Name = "Amount"
				Amount.Value = newData["Healing Potion"]
			end
		end
		
		if newData["Greater Healing Potion"] ~= nil then
			if newData["Greater Healing Potion"] > 0 then
				local GHP = Instance.new("StringValue")
				GHP.Parent = Folder
				GHP.Name = "Greater Healing Potion"
				local Amount = Instance.new("IntValue")
				Amount.Parent = GHP
				Amount.Name = "Amount"
				Amount.Value = newData["Greater Healing Potion"]
			end
		end		
		
		if newData["Magnus Shirt"] ~= nil then
			if newData["Magnus Shirt"] > 0 then
				local MS = Instance.new("StringValue")
				MS.Parent = Folder
				MS.Name = "Magnus Shirt"
				local Amount = Instance.new("IntValue")
				Amount.Parent = MS
				Amount.Name = "Amount"
				Amount.Value = newData["Magnus Shirt"]
			end
		end
		
		if newData["Magnus Pants"] ~= nil then
			if newData["Magnus Pants"] > 0 then
				local MPTS = Instance.new("StringValue")
				MPTS.Parent = Folder
				MPTS.Name = "Magnus Pants"
				local Amount = Instance.new("IntValue")
				Amount.Parent = MPTS
				Amount.Name = "Amount"
				Amount.Value = newData["Magnus Pants"]
			end
		end	
	end
end)

local function saveInventory(Player)
	
	print('SaveInventory has been called')
	
	local Inv = {}
	local Char = {}
	local Tools = {}
	
	for i, v in pairs(game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name):GetChildren()) do
		if v:FindFirstChild("Amount") then
			Inv[v.Name] = v.Amount.Value
		end
	end
	
	for i, v in pairs(game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot1:GetChildren()) do
		if v:IsA("StringValue") then
			Char[v.Name] = v.Value
		end
		for i, v2 in pairs(v:GetChildren()) do
			if v:IsA("IntValue") then
				Char[v.Name] = v.Amount.Value
			end			
		end
	end	
	
	for i, v in pairs(game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot2:GetChildren()) do
		if v:IsA("StringValue") then
			Char[v.Name] = v.Value
		end
		for i, v2 in pairs(v:GetChildren()) do
			if v:IsA("IntValue") then
				Char[v.Name] = v.Amount.Value
			end			
		end
	end	
	
	for i, v in pairs(game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot3:GetChildren()) do
		if v:IsA("StringValue") then
			Char[v.Name] = v.Value
		end
		for i, v2 in pairs(v:GetChildren()) do
			if v:IsA("IntValue") then
				Char[v.Name] = v.Amount.Value
			end			
		end
	end	
	
	for i, v in pairs(game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot4:GetChildren()) do
		if v:IsA("StringValue") then
			Char[v.Name] = v.Value
		end
		for i, v2 in pairs(v:GetChildren()) do
			if v:IsA("IntValue") then
				Char[v.Name] = v.Amount.Value
			end			
		end
	end	
	
	for i, v in pairs(game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot5:GetChildren()) do
		if v:IsA("StringValue") then
			Char[v.Name] = v.Value
		end
		for i, v2 in pairs(v:GetChildren()) do
			if v:IsA("IntValue") then
				Char[v.Name] = v.Amount.Value
			end			
		end
	end	
	
	for i, v in pairs(game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name).Tools.Slot6:GetChildren()) do
		if v:IsA("StringValue") then
			Char[v.Name] = v.Value
		end
		for i, v2 in pairs(v:GetChildren()) do
			if v:IsA("IntValue") then
				Char[v.Name] = v.Amount.Value
			end			
		end
	end	
	
	local suc, error = pcall(function()
		DataStore:SetAsync(Player.UserId, HTTP:JSONEncode(Inv))
	end)
	
	local suc1,error1 = pcall(function()
		SlotDataStore:SetAsync(Player.UserId, HTTP:JSONEncode(Char))
	end)
	
	if not suc then
		print(error.. ' Inventory Variable')
	end
	
	if not suc1 then
		print(error1.. ' Char Variable')
	end
end

game.Players.PlayerRemoving:Connect(function(Player)
	saveInventory(Player)
	
	if game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name) then
		game.ServerStorage.Assets.Inventory:FindFirstChild(Player.Name):Destroy()
	end
end)

game:BindToClose(function()
	for _, Client in ipairs(game:GetService("Players"):GetPlayers()) do
		spawn(function()
			saveInventory(Client)
		end)
	end
end)