How to add ScreenGUI to debounce?

  1. What do you want to achieve?
    The “Repeat” ScreenGUI I added to my shop is triggered when it isn’t supposed to be triggered. How do I fix this problem in my script?

  2. What is the issue?


    This pops up even though the player doesn’t actually own the item.
    Then the “Failure” message pops up (which is supposed to happen because the player doesn’t have enough to buy the item).

  3. What solutions have you tried so far?
    I’ve looked into how debounce works and have been trying to rewrite my script but it still doesn’t work :confused:

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

local debounce = false
script.Parent.MouseClick:Connect(function(player)
	if debounce == false then
		player.PlayerGui:FindFirstChild("Repeat") 
		local Repeat = script.Repeat:clone()
		Repeat.Parent = player.PlayerGui
		wait(2)
		Repeat:Destroy()
		
	else 
		
	end
	if not debounce then -- Here is the debounce!
		debounce = true
		if player.Data.Beli.Value >= RequiredGold then
		player.Data.Beli.Value = player.Data.Beli.Value - RequiredGold
		player.PlayerGui:FindFirstChild("Success") 
		local Success = script.Success:clone()
		Success.Parent = player.PlayerGui
		wait(2)
		tool:Clone().Parent = player.Backpack
		player.Character.Humanoid:UnequipTools()
		Success:Destroy()

	else 

		if player.Data.Beli.Value <= RequiredGold then
			player.PlayerGui:FindFirstChild("Failed")
			local Failed = script.Failed:clone()
			Failed.Parent = player.PlayerGui
			wait(2)
				Failed:Destroy()
			end
			debounce = false -- place is outside of the other function so it can restart the process.
		end
	end
end)

Screen Shot 2022-07-11 at 2.54.29 PM

I would really appreciate it if someone could help me fix this error in my script since I’m very new to scripting and don’t know what’s going wrong. :sweat_smile:

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

local debounce = false
script.Parent.MouseClick:Connect(function(player)
	if debounce == false then
        debounce = true
		player.PlayerGui:FindFirstChild("Repeat") 
		local Repeat = script.Repeat:clone()
		Repeat.Parent = player.PlayerGui
		wait(2)
		Repeat:Destroy()
        debounce = false		
	end

	if debounce then -- Here is the debounce!
		debounce = true
		if player.Data.Beli.Value >= RequiredGold then
		player.Data.Beli.Value = player.Data.Beli.Value - RequiredGold
		player.PlayerGui:FindFirstChild("Success") 
		local Success = script.Success:clone()
		Success.Parent = player.PlayerGui
		wait(2)
		tool:Clone().Parent = player.Backpack
		player.Character.Humanoid:UnequipTools()
		Success:Destroy()
        debounce = false

	else 

		if player.Data.Beli.Value <= RequiredGold then
            debounce = true
			player.PlayerGui:FindFirstChild("Failed")
			local Failed = script.Failed:clone()
			Failed.Parent = player.PlayerGui
			wait(2)
		    Failed:Destroy()
            debounce = false

			end

			debounce = false -- place is outside of the other function so it can restart the process.
		end
	end
end)

Just a few minor mistakes, nothing much!

2 Likes

Thank you for your reply!

I changed my script to this:

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

local debounce = false
script.Parent.MouseClick:Connect(function(player)
	if debounce == true then
		player.PlayerGui:FindFirstChild("Repeat") 
		local Repeat = script.Repeat:clone()
		Repeat.Parent = player.PlayerGui
		wait(2)
		Repeat:Destroy()
		
	else 
		
	end
	if not debounce then -- Here is the debounce!
		debounce = true
		if player.Data.Beli.Value >= RequiredGold then
		player.Data.Beli.Value = player.Data.Beli.Value - RequiredGold
		player.PlayerGui:FindFirstChild("Success") 
		local Success = script.Success:clone()
		Success.Parent = player.PlayerGui
		wait(2)
		tool:Clone().Parent = player.Backpack
		player.Character.Humanoid:UnequipTools()
		Success:Destroy()

	else 

		if player.Data.Beli.Value <= RequiredGold then
			player.PlayerGui:FindFirstChild("Failed")
			local Failed = script.Failed:clone()
			Failed.Parent = player.PlayerGui
			wait(2)
				Failed:Destroy()
			end
			debounce = false -- place is outside of the other function so it can restart the process.
		end
	end
end)

If I were to put

   debounce = false		
	end

at the end then it’d trigger the GUI even though the player doesn’t have the item so instead I put “else” and everything seems to be working right!

1 Like

Alright, well amazing, glad I was able to help, (even a little)!

If you need anything regarding this thread, you can always message me! And if you’d be so kind as to mark me as the solution, (to help the SEO), that would be fantastic!

Have an amazing rest of your day/night! :smiley:

2 Likes