Problem with Text Shop GUI

  1. What do you want to achieve?
    Shop GUI

  2. What is the issue?
    When clicked on a Purchased item, it shows “Purchase” but when clicked again it shows
    “Owned”

    Problem In Video:

  3. What solutions have you tried so far?
    I tried to search on the internet but none of results found.

local shopBtn = script.Parent:WaitForChild("ShopButton")
local invBtn = script.Parent:WaitForChild("InventoryButton")

local tweenService = game:GetService("TweenService")

local shopFrame = script.Parent:WaitForChild("ShopFrame")
local invFrame = script.Parent:WaitForChild("InventoryFrame")
local Sound = script.Parent.InventoryButton.Sound

local debounce = false

local UIPage = shopFrame.PagesWindow.UIPageLayout

local TrailPage = shopFrame.ListPages.TrailPage
local InventoryTrailPage = invFrame.ListPages.TrailPage
local InventoryScrollingTrail = invFrame.InventoryScrollingTrail

local ShopScrollingTrail = shopFrame.PagesWindow.TrailWindow.ShopScrollingTrail
local ShopScrollingSword = shopFrame.PagesWindow.SwordWindow.ShopScrollingSwords

local player = game.Players.LocalPlayer

local Notif = script.Parent.Notif

shopBtn.MouseButton1Click:Connect(function()
	Sound:Play()
	shopFrame.Visible = not shopFrame.Visible; invFrame.Visible = false
end)

invBtn.MouseButton1Click:Connect(function()
	Sound:Play()
	invFrame.Visible = not invFrame.Visible; shopFrame.Visible = false
	if Notif.Value == true then
		Notif.Value = false
		script.Parent.InventoryButton.Notification.Visible = false
	end
end)

function comma_value(amount)
	local formatted = amount
	while true do  
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
		if (k==0) then
			break
		end
	end
	return formatted
end

local trailsFolder = game.ReplicatedStorage:WaitForChild("Trails")
local ownedTrailsFolder = game.Players.LocalPlayer:WaitForChild("OwnedTrails")

local InfoItem = script.Parent.ShopFrame.PagesWindow.TrailWindow.TrailInfoItem
local InfoItem2 = script.Parent.InventoryFrame.TrailInfoItem

InfoItem.Button.MouseButton1Click:Connect(function()	
	
	Sound:Play()
	game.ReplicatedStorage.TrailSelectedRE:FireServer(true, InfoItem.ItemName.Text, script.Parent.NotificationBar, Notif)
	if  ownedTrailsFolder:FindFirstChild(InfoItem.ItemName.Text) then
		print("Owned")
		InfoItem.Button.BText.Text = "Owned"
	else
		InfoItem.Button.BText.Text = "Purchase"
		print("Not")
	end
end)

function updateInventory()

	for i, child in pairs(InventoryScrollingTrail:GetChildren()) do

		if child:IsA("TextButton") then child:Destroy() end
	end

	InfoItem2.Button.Visible = false
	InfoItem2.ViewportFrame.Visible = false
	InfoItem2.ItemName.Visible = false

	local ownedTrails = ownedTrailsFolder:GetChildren()

	table.sort(ownedTrails, function(a, b)
		return trailsFolder[a.Name].Price.Value < trailsFolder[b.Name].Price.Value or trailsFolder[a.Name].Price.Value == trailsFolder[b.Name].Price.Value and a.Name < b.Name
	end)

	for i, trail in pairs(ownedTrails) do

		local item = script.Item:Clone()

		item.Trail.UIGradient.Color = trail.Color

		item.Parent = InventoryScrollingTrail

		if game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart"):FindFirstChild(trail.Name) then
			InfoItem2.Button.BText.Text = "EQUIPPED"
		end		

		item.MouseButton1Click:Connect(function()
			Sound:Play()
			InfoItem2.ViewportFrame.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
			InfoItem2.ItemName.Text = trail.Name
			InfoItem2.ViewportFrame.UIGradient.Color = trail.Color

			InfoItem2.ViewportFrame.Visible = true
			InfoItem2.ItemName.Visible = true
			InfoItem2.Button.Visible = true
		end)
	end
end

InfoItem2.Button.MouseButton1Click:Connect(function()
	Sound:Play()
	game.ReplicatedStorage.TrailSelectedRE:FireServer(false, InfoItem2.ItemName.Text, script.Parent.NotificationBar, Notif)

	if InfoItem2.ItemName.Text ~= game.Players.LocalPlayer.CurrentTrail.Value then
		InfoItem2.Button.BText.Text = "EQUIP"
	else
		InfoItem2.Button.BText.Text = "EQUIPPED"
	end
end)


function updateShop()

	for _, child in pairs(ShopScrollingTrail:GetChildren()) do

		if child:IsA("TextButton") then child:Destroy() end
	end

	local shopTrails = trailsFolder:GetChildren()

	table.sort(shopTrails, function(a, b)
		return a.Price.Value < b.Price.Value or a.Price.Value == b.Price.Value and a.Name < b.Name
	end)

	for _, trail in pairs(shopTrails) do

		local item = script.Item:Clone()

		item.Trail.UIGradient.Color = trail.Color

		item.Parent = ShopScrollingTrail
		item.Name = trail.Name
		InfoItem.Button.BText.Text = "Purchase"

		item.MouseButton1Click:Connect(function()
			Sound:Play()

			if ownedTrailsFolder:FindFirstChild(InfoItem.ItemName.Text) and ownedTrailsFolder:FindFirstChild(trail.Name) then
				print("owned")
				InfoItem.Button.BText.Text = "Owned"
			else
				InfoItem.Button.BText.Text = "Purchase"
			end

			InfoItem.ViewportFrame.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
			InfoItem.ItemName.Text = trail.Name
			InfoItem.ViewportFrame.UIGradient.Color = trail.Color
			InfoItem.Price.Text = comma_value(trail.Price.Value) .. " Coins"
		end)
	end
end

game.ReplicatedStorage.TrailSelectedRE.OnClientEvent:Connect(function(plr)
	script.Parent.Notification:Play()
end)

updateShop()
updateInventory()

game.Players.LocalPlayer:WaitForChild("OwnedTrails").ChildAdded:Connect(function(item)

	local button = shopFrame.PagesWindow.TrailWindow.ShopScrollingTrail[item.Name]:Clone()
	button.Parent = script.Parent:WaitForChild("InventoryFrame").InventoryScrollingTrail

	button.MouseButton1Click:Connect(function()
		
		game.ReplicatedStorage.TrailSelectedRE:FireServer(false, item.Name, script.Parent.NotificationBar, Notif)

	end)

	local info = TweenInfo.new(0.5)
	local tween = tweenService:Create(script.Parent.NotificationBar, info, {TextTransparency = 1, BackgroundTransparency = 1, TextStrokeTransparency = 1 })
	local tween2 = tweenService:Create(script.Parent.NotificationBar.UIStroke, info, {Transparency = 1})

	script.Parent.Notification:Play()
	script.Parent.InventoryButton.Notification.Visible = true

	Notif.Value = true
	script.Parent.NotificationBar.Visible = true
	script.Parent.NotificationBar.Text = " Purchased New Item: <font color=\"rgb(85,255,0)\">" .. item.Name .. " Trail </font>"

	updateInventory(); updateShop()

	task.wait(3.5)
	tween2:Play()
	tween:Play()

	local NotificationBar = script.Parent.NotificationBar

	local function Invisible()
		NotificationBar.BackgroundTransparency = 0
		NotificationBar.TextTransparency = 0
		NotificationBar.TextStrokeTransparency = 0
		NotificationBar.UIStroke.Transparency = 0
		NotificationBar.Visible = false
	end

	tween.Completed:Connect(function()
		Invisible()
	end)

	tween2.Completed:Connect(function()
		Invisible()
	end)

end)
ownedTrailsFolder.ChildRemoved:Connect(function(item)
	updateInventory(); updateShop()
end)

trailsFolder.ChildAdded:Connect(updateShop)
trailsFolder.ChildRemoved:Connect(updateShop)

The problem code location (I think):

if ownedTrailsFolder:FindFirstChild(InfoItem.ItemName.Text) and ownedTrailsFolder:FindFirstChild(trail.Name) then
				print("owned")
				InfoItem.Button.BText.Text = "Owned"
			else
				InfoItem.Button.BText.Text = "Purchase"
			end

No erorrs on output

You are running the updateShop() function more than once (when every time a new trail gets added), and that creates more than one MouseButton1Click event for each item you have. I don’t know if it’s related to the current problem but I can tell you that this behaviour (of having multiple similar events) can also cause bugs.

Solution to prevent that behaviour: Use the updateShop() and any other function to only create the MouseButton1Click event for the new trail that is added. You can pass the new trail through the function argument.

How this will fix the problem?

As I said, it’s a solution to prevent that behaviour.

Oh alright. I willl try to figure it out

So good news I just figured it out

item.MouseButton1Click:Connect(function()
			Sound:Play()
                        InfoItem.Button.BText.Text = "Purchase" 

			InfoItem.ViewportFrame.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
			InfoItem.ItemName.Text = trail.Name
			InfoItem.ViewportFrame.UIGradient.Color = trail.Color
			InfoItem.Price.Text = comma_value(trail.Price.Value) .. " Coins"

item.MouseButton1Click:Connect(function()
			Sound:Play()

			if ownedTrailsFolder:FindFirstChild(InfoItem.ItemName.Text) and ownedTrailsFolder:FindFirstChild(trail.Name) then
				print("owned")
				InfoItem.Button.BText.Text = "Owned"
			else
				InfoItem.Button.BText.Text = "Purchase"
			end) 
		end)

I changed the position of mousebuttin1click and then put InfoItem text under Sound:Play() and it worked!

Thank you for assist me fixing this problem

1 Like