Ive been offline for quite a while because of game development. Recently, I implemented an inventory system, which resulted in a bucket load of bugs and unexpected behavior. I managed to clear out most of them, but there is one that is ruining that functionality of it. I recorded a video to present as evidence. video

Help?

Code:

local currentlyOn = script.Parent.Parent.Parent.Parent.Current
local formatTable ={
	["Font"] = "GothamBold",
	["TextColor3"] = Color3.new(1,1,1),
	["TextScaled"] = true		
}
local equippedTrail, equippedEffect

local Player = game.Players.LocalPlayer
local trailsButtons, effectsButtons 
local tableOfInfo = {}
function Setup()
	tableOfInfo = game.ReplicatedStorage.sendInventoryInformation:InvokeServer()
	local function RefreshScrollingFrame(Type)
		for _, item in ipairs(currentlyOn.Parent.Inventory.BackgroundForScrollingFrame.ScrollingFrame:GetChildren()) do
			if item.ClassName == "TextButton" then
				print("Found TextButton")
				item:Destroy()
			end
		end
		if Type == "Trails" then
			trailsButtons = {}
		elseif Type == "Effects" then
			effectsButtons = {}
		end
	end
	local function ReplaceItems(Table, Type)
		if Type == "Trails" then
			local Trails = Table.OwnedTrails
			for i,  v in pairs(game.ReplicatedStorage.trailsData:GetDescendants()) do
				for index,ownedTrail in pairs(Trails) do
					if v.Name == ownedTrail then
						local Button = Instance.new("TextButton")
						Button.Name = ownedTrail
						Button.Text = Button.Name.." Trail"
						Button.Parent = script.Parent
						local UiCorner = Instance.new("UICorner")
						UiCorner.Parent = Button
						local UIGradient = Instance.new("UIGradient")
						UIGradient.Parent =  Button
						UIGradient.Color = v.Color
						Button.TextScaled = formatTable.TextScaled
						Button.Font = formatTable.Font
						if v.Color == Color3.new(1,1,1) then
							Button.TextColor3 = Color3.new()
						else
							Button.TextColor3 = formatTable.TextColor3
						end
						table.insert(trailsButtons, #trailsButtons+1, Button)
					end
				end

			end
		elseif Type == "Effects"then
			local Effects =  Table.OwnedEffects
			for i,  v in pairs(game.ReplicatedStorage.effectsData:GetDescendants()) do
				for index, ownedEffect in pairs(Effects) do
					if v.Name == ownedEffect then
						local Button = Instance.new("TextButton")
						Button.Parent = script.Parent
						Button.Name = ownedEffect
						Button.Text = Button.Name.." Effect"
						local UiCorner = Instance.new("UICorner")
						UiCorner.Parent = Button
						local UIGradient = Instance.new("UIGradient")
						UIGradient.Parent =  Button
						UIGradient.Color = v.Color
						Button.TextScaled = formatTable.TextScaled
						Button.Font = formatTable.Font
						if v.Color == Color3.new(1,1,1) then
							Button.TextColor3 = Color3.new()
						else
							Button.TextColor3 = formatTable.TextColor3
						end
						table.insert(effectsButtons, #effectsButtons+1, Button)
					end
				end
			end
		end
	end

	RefreshScrollingFrame(currentlyOn.Value)
	tableOfInfo = game.ReplicatedStorage.sendInventoryInformation:InvokeServer()
	ReplaceItems(tableOfInfo, currentlyOn.Value)
	for index, button in pairs(trailsButtons) do
		for index, effectButton in pairs(effectsButtons) do
			effectButton.MouseButton1Click:Connect(function()
				local Preview = script.Parent.Parent.Parent.Preview.nameHere
				Preview.Text = effectButton.Text
				tableOfInfo = game.ReplicatedStorage.sendInventoryInformation:InvokeServer()
				local Equipped = tableOfInfo.EquippedEffect
				if Equipped == effectButton.Name then
					equippedEffect = Equipped

				end
				Preview.Parent.Equip.TextButton.MouseButton1Click:Connect(function()
					if  equippedEffect == effectButton.Name then
						Preview.Parent.Equip.TextButton.Text= "Unequip Effect"
						equippedEffect = effectButton.Name
						game.ReplicatedStorage.sendEquippedEffect:FireServer( true, equippedEffect)
					else
						equippedEffect = "White"
						game.ReplicatedStorage.UnequipEffect:FireServer(false, equippedEffect)
						Preview.Parent.Equip.TextButton.Text= "Equip Effect"
					end
				end)
			end)
			button.MouseButton1Click:Connect(function()
				local Preview = script.Parent.Parent.Parent.Preview.nameHere
				Preview.Text = button.Text
				tableOfInfo = game.ReplicatedStorage.sendInventoryInformation:InvokeServer()
				local Equipped = tableOfInfo.Equipped
				if Equipped == button.Name then
					equippedTrail = button.Name
				end
				Preview.Parent.Equip.TextButton.MouseButton1Click:Connect(function()
					if equippedTrail == button.Name then
						equippedTrail = button.Name
						game.ReplicatedStorage.sendEquipped:FireServer(true, equippedTrail)
						Preview.Parent.Equip.TextButton.Text= "Unequip Trail"
					else
						equippedTrail = "White"
						game.ReplicatedStorage.UnequipTrail:FireServer(false, equippedTrail)
						Preview.Parent.Equip.TextButton.Text= "Equip Trail"
					end
				end)
			end)
		end
	end
end







currentlyOn.Changed:Connect(function()
	Setup()
end)

I am unable to properly unequip/equip trails/effects. I don't know what is causing this.