Script Debounce Issue/What's wrong with my script?

  1. What do you want to achieve?

I want to have the equip button visible when the GarbageMode is enabled for artifacts and cards. Then, I want to have the equip button invisible for artifacts and have the equip button visible for cards when the GarbageMode is disabled.

In order to tell if the frame is a card, I had the script check to see whether x.Name is = game.ReplicatedStorage.Cards:FindFirstChild(x.Name).

  1. What is the issue?

It’s kinda hard to put into words so I have a video here to show you: garbagescriptissue.mov on Vimeo

Basically, when I press the Garbage button, the equip buttons are visible for both artifacts and cards and the EquipButton.Text = “Trash” (which is what I want); however, when I press the button again (to deactivate the GarbageMode), the EquipButton is still visible for artifacts and the EquipButton.Text doesn’t change back to “Equip” and stays as “Trash”

  1. What solutions have you tried so far?

I’ve created multiple scripts to test out different scripts/code but none of the scripts will work the way I want them to :frowning_face:


ClientScript within the InventoryGUI that handles the GarbageMode

local garbagebutton = script.Parent.SortFrame.Garbage

local debounce = false

garbagebutton.MouseButton1Click:Connect(function()
--	script.Parent.Items.Template.EquipButton.Visible = not script.Parent.Items.Template.EquipButton.Visible --added
	
	script.AddSound:Play()		
	if debounce == false then
		debounce = true
		
		for i, x in pairs(script.Parent.Items.Grid:GetChildren()) do
			if x:IsA("Frame") then
				if x:FindFirstChild("GarbageMode").Value == false then -- turn into garbagemode true
					x.GarbageMode.Value = true
					x.EquipButton.Text = "Trash" -- Turn it into "Drop" (This system just removes it)
					x.EquipButton.BackgroundColor3 = Color3.new(0.137255, 0.137255, 0.137255) -- color it changes to 
					
					x.EquipButton.Visible = true -- added
					
					x.EquipButton.MouseButton1Click:Connect(function()
					  if x.GarbageMode.Value == true then
					    script.AddSound:Play()
						game.ReplicatedStorage.InvEvents.RemoveFromInv:FireServer(x.Name)	
							print('fired')
						--	game.ReplicatedStorage.InvEvents.CardDeleted:FireServer()	
						--	if x.Name == game.ReplicatedStorage.Cards:FindFirstChild(item.Name) then --added	
							--end
						task.wait(0.1)
						x:Destroy()	
				      end
					end)
					
		
					--//IF you want to make the item drop, you'd do that code here
				end
			end
		end
			--/ Let's transofmr the inventory contents (Items) Equip button to become delete buttons :o
	else
	   debounce = false
			
		for i, x in pairs(script.Parent.Items.Grid:GetChildren()) do
			if x:IsA("Frame") then
				if x:FindFirstChild("GarbageMode").Value == true then -- turn into garbagemode true --if x:FindFirstChild("GarbageMode").Value == true then 
					x.GarbageMode.Value = false 

					if x.Name == game.ReplicatedStorage.Cards:FindFirstChild(x.Name) then --added
						--x.GarbageMode.Value = false 
						x.EquipButton.Text = "Equip" -- Turn it into "Drop" (This system just removes it)
						x.EquipButton.Visible = true
						x.EquipButton.BackgroundColor3 = Color3.new(0.368627, 0.298039, 0.298039) -- back to main color
end --added
					else
						if x.Name == game.ReplicatedStorage.Artifacts:FindFirstChild(x.Name) then --added
							--x.GarbageMode.Value = false 
							x.EquipButton.Visible = false

							--if game.ReplicatedStorage.Cards:FindFirstChild(item) then --added
							--	script.Parent.Items.Template.EquipButton.Visible = true
							x.EquipButton.Text = "Equip" -- Turn it into "Drop" (This system just removes it)
							x.EquipButton.BackgroundColor3 = Color3.new(0.368627, 0.298039, 0.298039) -- back to main color

						end
					end
				--end --added
			end

		end


	end
end)

Let me know if you need more information—I REALLY appreciate your help :sparkling_heart: :smiling_face:

1 Like

Sorry if I’m wrong But I think the error is on this line:

if x.Name == game.ReplicatedStorage.Cards:FindFirstChild(x.Name) then --added

and this line

if x.Name == game.ReplicatedStorage.Artifacts:FindFirstChild(x.Name) then --added

because x.Name will be string
but

game.ReplicatedStorage.Cards:FindFirstChild(x.Name)
and
game.ReplicatedStorage.Artifacts:FindFirstChild(x.Name)

will be object so if you wanna make it possible to detect if it was the same name you should do this

if x.Name == game.ReplicatedStorage.Cards:FindFirstChild(x.Name).Name then --added

and

if x.Name == game.ReplicatedStorage.Artifacts:FindFirstChild(x.Name).Name then --added

Sorry if I say something wrong.

1 Like

For some reason now the button won’t even let me press it…

Here’s the original script without any crazy edits:

How could I do what I would like to do using this script as a reference?



local garbagebutton = script.Parent.SortFrame.Garbage

local debounce = false


garbagebutton.MouseButton1Click:Connect(function()
	script.Parent.Items.Template.EquipButton.Visible = not script.Parent.Items.Template.EquipButton.Visible --added
	
	script.AddSound:Play()		
	if debounce == false then
		debounce = true
		
		for i, x in pairs(script.Parent.Items.Grid:GetChildren()) do
			if x:IsA("Frame") then
				if x:FindFirstChild("GarbageMode").Value == false then -- turn into garbagemode true
					x.GarbageMode.Value = true
					x.EquipButton.Text = "Trash" -- Turn it into "Drop" (This system just removes it)
					x.EquipButton.BackgroundColor3 = Color3.new(0.137255, 0.137255, 0.137255) -- color it changes to 
					
					
					x.EquipButton.MouseButton1Click:Connect(function()
					  if x.GarbageMode.Value == true then
					    script.AddSound:Play()
						game.ReplicatedStorage.InvEvents.RemoveFromInv:FireServer(x.Name)	
							print('fired')
						--	game.ReplicatedStorage.InvEvents.CardDeleted:FireServer()	
						--	if x.Name == game.ReplicatedStorage.Cards:FindFirstChild(item.Name) then --added	
							--end
						task.wait(0.1)
						x:Destroy()	
				      end
					end)
					
				end
			end
		end
			--/ Let's transofmr the inventory contents (Items) Equip button to become delete buttons :o
	else
	   debounce = false
			

		for i, x in pairs(script.Parent.Items.Grid:GetChildren()) do
			if x:IsA("Frame") then
				if x:FindFirstChild("GarbageMode").Value == true then -- turn into garbagemode true --if x:FindFirstChild("GarbageMode").Value == true then 
					x.GarbageMode.Value = false 
					
						--if game.ReplicatedStorage.Cards:FindFirstChild(item) then --added
						--	script.Parent.Items.Template.EquipButton.Visible = true
					
						x.EquipButton.Text = "Equip" -- Turn it into "Drop" (This system just removes it)
						x.EquipButton.BackgroundColor3 = Color3.new(0.368627, 0.298039, 0.298039) -- back to main color
				
				    end
			end
			
		end
			
		 	
	end
end)

EDIT: I figured out why I couldn’t press the button—script still doesn’t work though

I think you need to change the value of the debounce to true after task.wait
Try that and give me an update.

task.wait(0.1)
x:Destroy()
-- add custom cooldown for the debounce
task.wait(yourcooldownhere)
debounce = true
1 Like

Here’s a video of the issue I’m having now after changing the script: https://vimeo.com/814500893

Here’s the script I’m using:

local garbagebutton = script.Parent.SortFrame.Garbage

local debounce = false


garbagebutton.MouseButton1Click:Connect(function()
	script.Parent.Items.Template.EquipButton.Visible = not script.Parent.Items.Template.EquipButton.Visible --added
	
	script.AddSound:Play()		
	if debounce == false then
		debounce = true
		
		for i, x in pairs(script.Parent.Items.Grid:GetChildren()) do
			if x:IsA("Frame") then
				if x:FindFirstChild("GarbageMode").Value == false then -- turn into garbagemode true
					x.GarbageMode.Value = true
					x.EquipButton.Text = "Trash" -- Turn it into "Drop" (This system just removes it)
					x.EquipButton.BackgroundColor3 = Color3.new(0.137255, 0.137255, 0.137255) -- color it changes to 
					
					x.EquipButton.Visible = true -- added
					
					x.EquipButton.MouseButton1Click:Connect(function()
					  if x.GarbageMode.Value == true then
					    script.AddSound:Play()
						game.ReplicatedStorage.InvEvents.RemoveFromInv:FireServer(x.Name)	
							print('fired')
						--	game.ReplicatedStorage.InvEvents.CardDeleted:FireServer()	
						--	if x.Name == game.ReplicatedStorage.Cards:FindFirstChild(item.Name) then --added	
							--end
						task.wait(0.1)
						x:Destroy()	
				      end
					end)
					
					task.wait(.2)
					debounce = true
					--//IF you want to make the item drop, you'd do that code here
				end
			end
		end
			--/ Let's transofmr the inventory contents (Items) Equip button to become delete buttons :o
	else
	   debounce = false
			
		for i, x in pairs(script.Parent.Items.Grid:GetChildren()) do
			if x:IsA("Frame") then
				if x:FindFirstChild("GarbageMode").Value == true then -- turn into garbagemode true
					x.GarbageMode.Value = false 
					x.EquipButton.Text = "Equip" -- Turn it into "Drop" (This system just removes it)
					x.EquipButton.BackgroundColor3 = Color3.new(0.368627, 0.298039, 0.298039)
					
					if x.Name.Name == game.ReplicatedStorage.Cards:FindFirstChild(x.Name) then
						x.EquipButton.Text = "Equip"
						--x.EquipButton.Visible = true -- remove this line
						x.EquipButton.BackgroundColor3 = Color3.new(0.368627, 0.298039, 0.298039)
					else		
						-- check if item is an artifact and set EquipButton.Visible to false
						if x.Name == game.ReplicatedStorage.Artifacts:FindFirstChild(x.Name) then
							x.EquipButton.Visible = false
						end
					end
				end
			end
		end
		end
				end)

In my opinion I can’t see anything that can made your script got error, So make sure to set all GarbageMode.Value in all x to false before run the game + make sure you typed the correct object name that you are using in script and in for loop if you put some wait function it will make your code delay example: if there’re 2 items in your inventory when you click Delete Items, First item will changed equip text to trash text then wait cooldown time(0.2) then it will change second item equip text to trash text.

also you can use Debris service instead using :Destroy()

example:
local Debris = game:GetService(“Debris”)
DB:AddItem(x, 0) – x stand for object that you wanna destroy and second argument is lifetime or delay time before it got destroyed

1 Like

The issue is that this is not what I’m trying to accomplish…

In your lastest code you should change this:

if x.Name.Name == game.ReplicatedStorage.Cards:FindFirstChild(x.Name) then
						x.EquipButton.Text = "Equip"
						--x.EquipButton.Visible = true -- remove this line
						x.EquipButton.BackgroundColor3 = Color3.new(0.368627, 0.298039, 0.298039)
					else		
						-- check if item is an artifact and set EquipButton.Visible to false
						if x.Name == game.ReplicatedStorage.Artifacts:FindFirstChild(x.Name) then
							x.EquipButton.Visible = false
						end
					end

To this:

if x.Name == game.ReplicatedStorage.Cards:FindFirstChild(x.Name).Name then
	x.EquipButton.Text = "Equip"
	--x.EquipButton.Visible = true -- remove this line
	x.EquipButton.BackgroundColor3 = Color3.new(0.368627, 0.298039, 0.298039)
elseif x.Name == game.ReplicatedStorage.Artifacts:FindFirstChild(x.Name).Name then
	-- check if item is an artifact and set EquipButton.Visible to false
	x.EquipButton.Visible = false
end

also try that and give me an update

1 Like

Here’s an update video!

https://vimeo.com/814504871

And here’s the exact script:

local garbagebutton = script.Parent.SortFrame.Garbage

local debounce = false


garbagebutton.MouseButton1Click:Connect(function()
	script.Parent.Items.Template.EquipButton.Visible = not script.Parent.Items.Template.EquipButton.Visible --added
	
	script.AddSound:Play()		
	if debounce == false then
		debounce = true
		
		for i, x in pairs(script.Parent.Items.Grid:GetChildren()) do
			if x:IsA("Frame") then
				if x:FindFirstChild("GarbageMode").Value == false then -- turn into garbagemode true
					x.GarbageMode.Value = true
					x.EquipButton.Text = "Trash" -- Turn it into "Drop" (This system just removes it)
					x.EquipButton.BackgroundColor3 = Color3.new(0.137255, 0.137255, 0.137255) -- color it changes to 
					
					x.EquipButton.Visible = true -- added
					
					x.EquipButton.MouseButton1Click:Connect(function()
					  if x.GarbageMode.Value == true then
					    script.AddSound:Play()
						game.ReplicatedStorage.InvEvents.RemoveFromInv:FireServer(x.Name)	
							print('fired')
						--	game.ReplicatedStorage.InvEvents.CardDeleted:FireServer()	
						--	if x.Name == game.ReplicatedStorage.Cards:FindFirstChild(item.Name) then --added	
							--end
						task.wait(0.1)
						x:Destroy()	
				      end
					end)
					
					task.wait(.2)
					debounce = true
					--//IF you want to make the item drop, you'd do that code here
				end
			end
		end
			--/ Let's transofmr the inventory contents (Items) Equip button to become delete buttons :o
	else
	   debounce = false
			
		for i, x in pairs(script.Parent.Items.Grid:GetChildren()) do
			if x:IsA("Frame") then
				if x:FindFirstChild("GarbageMode").Value == true then -- turn into garbagemode true
					x.GarbageMode.Value = false 
					x.EquipButton.Text = "Equip" -- Turn it into "Drop" (This system just removes it)
					x.EquipButton.BackgroundColor3 = Color3.new(0.368627, 0.298039, 0.298039)
					
					if x.Name == game.ReplicatedStorage.Cards:FindFirstChild(x.Name).Name then

						x.EquipButton.Text = "Equip"

						--x.EquipButton.Visible = true -- remove this line

						x.EquipButton.BackgroundColor3 = Color3.new(0.368627, 0.298039, 0.298039)

					elseif x.Name == game.ReplicatedStorage.Artifacts:FindFirstChild(x.Name).Name then

						-- check if item is an artifact and set EquipButton.Visible to false

						x.EquipButton.Visible = false

				
					end
				end
			end
		end
		end
				end)

I’m also open to doing TeamCreate, if you’d prefer to do that instead!

I think you dont need the first else, on line 43.
You should handle all behaviour from inside the first statement, and based on the (“GarbageMode”).Value different behaviour will happen when pressing the button. To equip or destroy.
And probably with only one (“GarbageMode”).Value, not one into each Item/button

1 Like

Can you try this?

for i, x in pairs(script.Parent.Items.Grid:GetChildren()) do
			if x:IsA("Frame") then
				if x:FindFirstChild("GarbageMode").Value == true then -- turn into garbagemode true
					x.GarbageMode.Value = false
					x.EquipButton.BackgroundColor3 = Color3.new(0.368627, 0.298039, 0.298039)

					if game.ReplicatedStorage.Cards:FindFirstChild(x.Name) then

						x.EquipButton.Text = "Equip"

						--x.EquipButton.Visible = true -- remove this line
					end

					if game.ReplicatedStorage.Artifacts:FindFirstChild(x.Name) then

						-- check if item is an artifact and set EquipButton.Visible to false

						x.EquipButton.Visible = false
					end
				end
			end
		end