ScreenGUIs not appearing

Summary: “Failed” ScreenGUI not appearing when part clicked and $ = 0 & both (“success” & “failed”) ScreenGUI’s showing up when only one should appear.

The “Failed to purchase” message for my shop doesn’t work correctly. When the player clicks on it with $0, it doesn’t show up. But when the player has enough $ to purchase, the success prompts shows up, but then the “Failed to purchase” prompt appears a few seconds afterward (even though the player has enough $).

local RequiredGold = script.Parent.Parent.Parent.Price.Value
local player = game.Players.LocalPlayer
local tool = game.ServerStorage.Shop["Health Potion"]

script.Parent.MouseClick:Connect(function(player)
	if player.Data.Beli.Value >= RequiredGold then
		player.Data.Beli.Value = player.Data.Beli.Value - RequiredGold
		if not player.PlayerGui:FindFirstChild("Success") then
		local Success = script.Success:clone()
		Success.Parent = player.PlayerGui
			wait(2)
			tool:Clone().Parent = player.Backpack
		player.Character.Humanoid:UnequipTools()
		Success:Destroy()
		end
		if not player.PlayerGui:FindFirstChild("Failed") then
		local Failed = script.Failed:clone()
		Failed.Parent = player.PlayerGui
		wait(2)
		Failed:Destroy()
		end
	end
	wait(2)
end)	

Screen Shot 2022-07-05 at 8.32.18 AM

Is there something I should add to my script to correct this bug/error?

Thank you for your help! :slight_smile:

if player.Data.Beli.Value >= RequiredGold then

This 'if' condition 'then' block should contain an else expression that handles situations where the condition evaluates to false, the code that displays the ‘Failed’ prompt should be placed inside this else expression. Here’s some pseudo-code to give you a general idea.

if EnoughMoney then
	--Show success prompt.
else
	--Show fail prompt.
end
2 Likes

Super helpful! :slight_smile: Thank you so much for your help!