Script broken for some reason (weird glitch)

wait(1)

local Model = workspace["2R6"]
local hats = workspace.HATS:GetChildren()
local items = workspace.ITEMS:GetChildren()
local randomitem = workspace.ITEMS
local thumbnailUrl = nil
local MarketplaceService = game:GetService("MarketplaceService")
local SSS = game:GetService("ServerScriptService")
local module = require(SSS.Items)
local ITEMRAP = 0
local player = script.Parent.Parent.Parent.Parent.Parent
local buying = 1
local selling = 0
local hatsowned = player.Backpack:GetChildren()
local bp = player.Backpack
local selected = 1
local itemID = 1337
local randomHat = 1337
local alrdone = true
local clonedhat

function customerchange ()
	
	wait()
	
	print("detected")
	
	wait()
	if #bp:GetChildren() > 0 then
		alrdone = false
		randomHat = bp:GetChildren()[math.random(1, #bp:GetChildren())]
		randomitem = hats[math.random(1, #hats)]
		print(randomHat.Name)
		itemID = randomHat.Name


		local itprdct = MarketplaceService:GetProductInfo(randomHat.Name)
		local img = script.Parent
		clonedhat = randomitem:Clone()


		script.Parent.Parent:WaitForChild("nameofitem").Text = itprdct.Name

		thumbnailUrl = "https://www.roblox.com/asset-thumbnail/image?assetId=" .. itemID .. "&width=420&height=420&format=png"

		img.Image = thumbnailUrl


		print("1337")

		Model.Humanoid:AddAccessory(clonedhat)
		Model.Head.BrickColor = BrickColor.random()
		Model.Torso.BrickColor = BrickColor.random()
		Model["Left Arm"].BrickColor = BrickColor.random()
		Model["Left Leg"].BrickColor = BrickColor.random()
		Model["Right Arm"].BrickColor = BrickColor.random()
		Model["Right Leg"].BrickColor = BrickColor.random()
	end

end


function GetItemDetails()
	local success, item, name, acronym, rap, value, defaultValue, demand, trend, projected, hyped, rare
		= module.fetchItemDetails(itemID)
	if success then
		ITEMRAP = defaultValue * math.random(90, 110) / 100
		youthink = defaultValue * 0.99
		youthink2 = defaultValue * 1.01
	end
end

repeat
	script.Parent.Parent.Visible = false
	wait()
until #bp:GetChildren() > 0

customerchange()

GetItemDetails()

script.Parent.Parent:WaitForChild("priceofitem").Text = "R$ " .. ITEMRAP 
script.Parent.Parent:WaitForChild("TextLabel").Text = "You think that this item is worth around R$ " .. youthink .. " to R$ " .. youthink2

script.Parent.Parent.Accept.Accepted.OnServerEvent:Connect(function()
	
	
	player.leaderstats.ROBUX.Value += ITEMRAP
	script.Parent.Parent:WaitForChild("priceofitem").Text = "R$ " .. ITEMRAP 
	script.Parent.Parent:WaitForChild("TextLabel").Text = "You think that this item is worth around R$ " .. youthink .. " to R$ " .. youthink2
	
	randomHat:Destroy()
	
	wait()
	alrdone = false
	

end)


script.Parent.Parent.Decline.Declined.OnServerEvent:Connect(function()
	
	script.Parent.Parent.Parent.WaitBro.Value = 1
	
	script.Parent.Parent.Visible = false
	
	alrdone = false
	
	script.Parent.Parent:WaitForChild("priceofitem").Text = "R$ " .. ITEMRAP 
	script.Parent.Parent:WaitForChild("TextLabel").Text = "You think that this item is worth around R$ " .. youthink .. " to R$ " .. youthink2
end)

while true do

	if #bp:GetChildren() >0 then
		script.Parent.Parent.Visible = true
		Model.Head.face.Transparency = 0
		Model.Head.Transparency = 0
		Model.Torso.Transparency = 0
		Model["Left Arm"].Transparency = 0
		Model["Left Leg"].Transparency = 0
		Model["Right Arm"].Transparency = 0
		Model["Right Leg"].Transparency = 0
		wait()
	else
		script.Parent.Parent.Visible = false
		Model.Head.face.Transparency = 1
		Model.Head.Transparency = 1
		Model.Torso.Transparency = 1
		Model["Left Arm"].Transparency = 1
		Model["Left Leg"].Transparency = 1
		Model["Right Arm"].Transparency = 1
		Model["Right Leg"].Transparency = 1
	
		wait()
	end

	
	if not alrdone then
		clonedhat:Destroy()
		customerchange()
		GetItemDetails()
		script.Parent.Parent:WaitForChild("priceofitem").Text = "R$ " .. ITEMRAP 
		script.Parent.Parent:WaitForChild("TextLabel").Text = "You think that this item is worth around R$ " .. youthink .. " to R$ " .. youthink2
		alrdone = true
		

	end
	
end

the code might seem complicated but help me out guys

External Media

as you guys see in the recording… after selling an item and buying a new item it still shows the old sold item on the gui and if i try to sell the item it actually gets sold even though i do not own the item and right after the item i bought after selling that item goes in to the gui…

this wasn’t happening before i modified something that i forgot from yesterday and it started to glitch as you can see

i’d appreciate any of you guys comments and thoughts