So I have a script that lets you click on an item in the inventory to put it into the selected items menu, it works to select and un select items: https://gyazo.com/f00639aa124a679a5800fd6a6bf184ab, but when you close the inventory and reopen it, it doesn't work how it should: https://gyazo.com/998891ac3a43372c96d5e382ebde7046, even when the item is selected it adds it rather than removing it. This is the script that selects the item:

local clicks = 0

script.Parent.Parent.MouseButton1Click:connect(function()
	clicks = clicks + 1
	local SelectedUnits = script.Parent.Parent.Parent.Parent.Parent.Units.SelectedUnits:GetChildren()
	local Selected = false
	if clicks/2 == math.floor(clicks/2) then
		Selected = true
	end
	if Selected == false then
		local Unit = Instance.new("StringValue")
		Unit.Name = script.Parent.Unit.Value
		Unit.Value = script.Parent.Unit.Value
		Unit.Parent = script.Parent.Parent.Parent.Parent.Parent.Units.SelectedUnits
		script.Parent.BorderSizePixel = 5
	elseif Selected == true then
		script.Parent.BorderSizePixel = 0
		local unit = script.Parent.Parent.Parent.Parent.Parent.Units.SelectedUnits:FindFirstChild(script.Parent.Unit.Value)
		unit:Destroy()
	end
end)

And this is the script that loads the selected items into the bottom selected item menu:

script.Parent.SelectedUnits.ChildAdded:connect(function()
	for number, thing in pairs (script.Parent:GetChildren()) do
		if thing:IsA("Frame") then
			for i,v in pairs (thing:GetChildren()) do
				v:Destroy()
			end
		end
	end
	local unitlist = {}
	local unitval = script.Parent.SelectedUnits:GetChildren()
	for i,v in pairs (unitval) do
		if v:IsA("StringValue") then
			table.insert(unitlist, v)
		end
	end
	local player = script.Parent.Parent.Parent.Parent
	wait(0.2)
	if unitlist[1] then
	for number, unitname in pairs (unitlist) do
		local unit = game.ServerStorage.UNITS[unitname.Name]
		local SpawnGui = game.ServerStorage.SpawnGui:Clone()
		SpawnGui.Parent = script.Parent["Unit"..number]
		SpawnGui.Unit.Value = unit.Name
		SpawnGui.Image = unit.Stats.RespawnGuiImageId.Value
		SpawnGui.BorderColor3 = unit.Stats.RespawnGuiBorderColor.Value
		SpawnGui.UnitName.TextColor3 = unit.Stats.RespawnGuiTextColor.Value
		SpawnGui.UnitName.TextStrokeColor3 = unit.Stats.RespawnGuiTextOutlineColor.Value
		SpawnGui.UnitName.Text = unit.Name
	end
	end
end)

script.Parent.SelectedUnits.ChildRemoved:connect(function()
	for number, thing in pairs (script.Parent:GetChildren()) do
		if thing:IsA("Frame") then
			for i,v in pairs (thing:GetChildren()) do
				v:Destroy()
			end
		end
	end
	local unitlist = {}
	local unitval = script.Parent.SelectedUnits:GetChildren()
	for i,v in pairs (unitval) do
		if v:IsA("StringValue") then
			table.insert(unitlist, v)
		end
	end
	local player = script.Parent.Parent.Parent.Parent
	wait(0.2)
	if unitlist[1] then
	for number, unitname in pairs (unitlist) do
		local unit = game.ServerStorage.UNITS[unitname.Value]
		local SpawnGui = game.ServerStorage.SpawnGui:Clone()
		SpawnGui.Parent = script.Parent["Unit"..number]
		SpawnGui.Unit.Value = unit.Name
		SpawnGui.Image = unit.Stats.RespawnGuiImageId.Value
		SpawnGui.BorderColor3 = unit.Stats.RespawnGuiBorderColor.Value
		SpawnGui.UnitName.TextColor3 = unit.Stats.RespawnGuiTextColor.Value
		SpawnGui.UnitName.TextStrokeColor3 = unit.Stats.RespawnGuiTextOutlineColor.Value
		SpawnGui.UnitName.Text = unit.Name
	end
	end
end)

These are the scrips that close and open the inventory:

script.Parent.MouseButton1Click:connect(function()
	if script.Parent.Parent.InventoryOpen.Value == false then
		script.Parent.Parent.InventoryOpen.Value = true
		script.Parent.Parent.Inventory.Visible = true
		script.Parent.Parent.SelectedUnitStats.Visible = true
		script.Parent.Parent.CloseButton.Visible = true
	end
end)

and

script.Parent.MouseButton1Click:connect(function()
	if script.Parent.Parent.InventoryOpen.Value == true then
		script.Parent.Parent.InventoryOpen.Value = false
		script.Parent.Parent.Inventory.Visible = false
		script.Parent.Parent.SelectedUnitStats.Visible = false
		script.Parent.Parent.CloseButton.Visible = false
	end
end)

And this is the script that loads the items in the inventory when it's opened or closed:

wait(5)

script.Parent.Parent.InventoryOpen.Changed:connect(function(vis)
	if vis == true then
		local unitlist = {}
		local unitval = game.ServerStorage.SAVEDDATA[script.Parent.Parent.Parent.Parent.Parent.Name].Units:GetChildren()
		for i,v in pairs (unitval) do
			unitlist[i] = v.Value
		end
		for number, unitname in pairs (unitlist) do
			local unit = game.ServerStorage.UNITS[unitname]
			local ItemGui = game.ServerStorage.InventoryItemGui:Clone()
			ItemGui.Name = unitname
			ItemGui.Parent = script.Parent
			local Ypos = ((number - 1)*83)
			ItemGui.Position = UDim2.new(0,10,0,Ypos)
			ItemGui.Frame.Unit.Value = unitname
			ItemGui.Frame.ImageLabel.Image = unit.Stats.RespawnGuiImageId.Value
			ItemGui.Frame.UnitName.Text = unitname
			ItemGui.Frame.UnitType.Text = unit.Stats.Type.Value
			ItemGui.Frame.UnitName.TextColor3 = unit.Stats.RespawnGuiTextColor.Value
			ItemGui.Frame.UnitName.TextStrokeColor3 = unit.Stats.RespawnGuiTextOutlineColor.Value
			ItemGui.Frame.BackgroundColor3 = unit.Stats.RespawnGuiBorderColor.Value
			if number/4 then
				script.Parent.CanvasSize = UDim2.new(0,0,(number/4 + 1),0)
			end
			local Selected = false
			for i,v in pairs (script.Parent.Parent.Parent.Units.SelectedUnits:GetChildren()) do
				if v.Value == unitname then
					Selected = true
				end
			end
			if Selected == true then
				ItemGui.Frame.BorderSizePixel = 5
			end
		end
	end
end)

script.Parent.Parent.InventoryOpen.Changed:connect(function(vis)
	if vis == false then
		local units = script.Parent:GetChildren()
		for i,v in pairs (units) do
			if v:IsA("ImageButton") then
				v:Destroy()
			end
		end
	end
end)

I just have no idea how to fix this..