How to stop a normal click function firing more than once?

I have this client-side code, it fires the function more than once when the “yes” button is clicked here is the full code:

game.ReplicatedStorage.Eggs.EggAreYouSure.OnClientEvent:Connect(function(egg, prox, cost, pet)
	local debounce = false
	local plr = game.Players.LocalPlayer
	print(egg)
	print(prox)
	print(cost)
	
	print("For loop started")
	print(prox.Parent.Parent)

	if script.Parent.Visible == false and debounce == false then
		script.Parent.Visible = true
		script.Parent.Frame.TextLabel.Text = tostring("Are you sure you want to buy ".. egg.Name .. " for "..cost.." Gold?")
		

		script.Parent.Frame.Yes.TextButton.MouseButton1Click:Connect(function()
			if plr.leaderstats.Gold.Value >= cost then
				print(pet)
				local newPet = game.ReplicatedStorage.SingularPet:Clone()
				newPet.Parent = plr.PlayerGui.InventoryGui.PetsUI.PetsFront.ScrollingFrame
				local modelPet = game.ReplicatedStorage.AllItems.AllPets:FindFirstChild(pet)
				if modelPet then
					newPet.ImageButton.Image = modelPet.Id.Value
				end
				game.ReplicatedStorage.Eggs.EggAreYouSure:FireServer(cost)
				--Sounds
				sound:PlayLocalSound(script.Ching)
				sound:PlayLocalSound(script.Click)
				
				script.Parent.Visible = false
				debounce = true
				local egg = script.Parent.Parent.Parent.Egg.ImageLabel
				print("Bought")
				egg.Visible = true
				
				--Tweens
				egg:TweenSize(UDim2.new(0.7, 0, 0.7, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.5)
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {Rotation = 20}):Play()
				sound:PlayLocalSound(script.EggCrack)
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {Rotation = 0}):Play()
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {Rotation = -30}):Play()
				sound:PlayLocalSound(script.EggCrack)
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {Rotation = 0}):Play()
				egg:TweenSize(UDim2.new(0.1, 0, 0.1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.5)
				
		
				task.wait(0.5)
				egg.Visible = false
			else
				script.Parent.Visible = false
			end
		end)
		
		script.Parent.Frame.No.TextButton.MouseButton1Click:Connect(function()
			script.Parent.Visible = false
			sound:PlayLocalSound(script.Click)
		end)
	end
end)

here is the part that i think has the problem

script.Parent.Frame.Yes.TextButton.MouseButton1Click:Connect(function()
			if plr.leaderstats.Gold.Value >= cost then
				print(pet)
				local newPet = game.ReplicatedStorage.SingularPet:Clone()
				newPet.Parent = plr.PlayerGui.InventoryGui.PetsUI.PetsFront.ScrollingFrame
				local modelPet = game.ReplicatedStorage.AllItems.AllPets:FindFirstChild(pet)
				if modelPet then
					newPet.ImageButton.Image = modelPet.Id.Value
				end
				game.ReplicatedStorage.Eggs.EggAreYouSure:FireServer(cost)
				--Sounds
				sound:PlayLocalSound(script.Ching)
				sound:PlayLocalSound(script.Click)
				
				script.Parent.Visible = false
				debounce = true
				local egg = script.Parent.Parent.Parent.Egg.ImageLabel
				print("Bought")
				egg.Visible = true
				
				--Tweens
				egg:TweenSize(UDim2.new(0.7, 0, 0.7, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.5)
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {Rotation = 20}):Play()
				sound:PlayLocalSound(script.EggCrack)
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {Rotation = 0}):Play()
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {Rotation = -30}):Play()
				sound:PlayLocalSound(script.EggCrack)
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {Rotation = 0}):Play()
				egg:TweenSize(UDim2.new(0.1, 0, 0.1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.5)
				
		
				task.wait(0.5)
				egg.Visible = false
			else
				script.Parent.Visible = false
			end
		end)
1 Like

Providing more information could help, but from what I can understand, just add a boolean variable, lets call it fireOnce, then we check at the start of the function if it is true then we can set it to false.
It would look something like this:

local fireOnce = true
script.Parent.Triggered:Connect(function(plr)
  if fireOnce then
    fireOnce = false

    --You function code

  end
end)
1 Like

I got where the problem was wrong sorry! It is a different issue with a click function I think.

yeah you could do the same thing there too, if you want it to fire again after the function is complete just set the boolean value to true at the end

I dont want it to fire again after i want the function to fire once but the click function fires twice if its the 2nd time buying 3 times if its 3rd time etc

Yeah I read the code and what you’re basically doing is creating a new function again and again, to fix it just disconnect the function after it is run, like:

local func = script.Parent.Frame.Yes.TextButton.MouseButton1Click:Connect(function()
  --code
end)
func:Disconnect()

		script.Parent.Frame.Yes.TextButton.MouseButton1Click:Connect(yesEggClick(plr, pet, egg, cost, debounce))
			
end)

so turn it into a local function like this?

game.ReplicatedStorage.Eggs.EggAreYouSure.OnClientEvent:Connect(function(egg, prox, cost, pet)
	local debounce = false
	local plr = game.Players.LocalPlayer
	print(egg)
	print(prox)
	print(cost)
	
	print("For loop started")
	print(prox.Parent.Parent)

	if script.Parent.Visible == false and debounce == false then
		script.Parent.Visible = true
		script.Parent.Frame.TextLabel.Text = tostring("Are you sure you want to buy ".. egg.Name .. " for "..cost.." Gold?")
		

		local func = script.Parent.Frame.Yes.TextButton.MouseButton1Click:Connect(function()
			if plr.leaderstats.Gold.Value >= cost then
				print(pet)
				local newPet = game.ReplicatedStorage.SingularPet:Clone()
				newPet.Parent = plr.PlayerGui.InventoryGui.PetsUI.PetsFront.ScrollingFrame
				local modelPet = game.ReplicatedStorage.AllItems.AllPets:FindFirstChild(pet)
				if modelPet then
					newPet.ImageButton.Image = modelPet.Id.Value
				end
				game.ReplicatedStorage.Eggs.EggAreYouSure:FireServer(cost)
				--Sounds
				sound:PlayLocalSound(script.Ching)
				sound:PlayLocalSound(script.Click)
				
				script.Parent.Visible = false
				debounce = true
				local egg = script.Parent.Parent.Parent.Egg.ImageLabel
				print("Bought")
				egg.Visible = true
				
				--Tweens
				egg:TweenSize(UDim2.new(0.7, 0, 0.7, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.5)
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {Rotation = 20}):Play()
				sound:PlayLocalSound(script.EggCrack)
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {Rotation = 0}):Play()
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {Rotation = -30}):Play()
				sound:PlayLocalSound(script.EggCrack)
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {Rotation = 0}):Play()
				egg:TweenSize(UDim2.new(0.1, 0, 0.1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.5)
				
		
				task.wait(0.5)
				egg.Visible = false
			else
				script.Parent.Visible = false
			end
		end)

        func:Disconnect()
		
		local func2 = script.Parent.Frame.No.TextButton.MouseButton1Click:Connect(function()
			script.Parent.Visible = false
			sound:PlayLocalSound(script.Click)
		end)

          func2:Disconnect()

	end
end)

the functions arent called in the first place

oh I see, your code structure is very different, here is the fixed version



game.ReplicatedStorage.Eggs.EggAreYouSure.OnClientEvent:Connect(function(egg, prox, cost, pet)

	local plr = game.Players.LocalPlayer
    local func, func2

	print(egg)
	print(prox)
	print(cost)
	
	print("For loop started")
	print(prox.Parent.Parent)

	if script.Parent.Visible == false then
		script.Parent.Visible = true
		script.Parent.Frame.TextLabel.Text = tostring("Are you sure you want to buy ".. egg.Name .. " for "..cost.." Gold?")
		

		func = script.Parent.Frame.Yes.TextButton.MouseButton1Click:Connect(function()
			if plr.leaderstats.Gold.Value >= cost then
				print(pet)
				local newPet = game.ReplicatedStorage.SingularPet:Clone()
				newPet.Parent = plr.PlayerGui.InventoryGui.PetsUI.PetsFront.ScrollingFrame
				local modelPet = game.ReplicatedStorage.AllItems.AllPets:FindFirstChild(pet)
				if modelPet then
					newPet.ImageButton.Image = modelPet.Id.Value
				end
				game.ReplicatedStorage.Eggs.EggAreYouSure:FireServer(cost)
				--Sounds
				sound:PlayLocalSound(script.Ching)
				sound:PlayLocalSound(script.Click)
				
				script.Parent.Visible = false
				debounce = true
				local egg = script.Parent.Parent.Parent.Egg.ImageLabel
				print("Bought")
				egg.Visible = true
				
				--Tweens
				egg:TweenSize(UDim2.new(0.7, 0, 0.7, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.5)
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {Rotation = 20}):Play()
				sound:PlayLocalSound(script.EggCrack)
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {Rotation = 0}):Play()
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {Rotation = -30}):Play()
				sound:PlayLocalSound(script.EggCrack)
				task.wait(0.5)
				tween:Create(egg, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {Rotation = 0}):Play()
				egg:TweenSize(UDim2.new(0.1, 0, 0.1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.5)
				
		
				task.wait(0.5)
				egg.Visible = false
			else
				script.Parent.Visible = false
			end
                 func:Disconnect()
                 func2:Disconnect()
		end)
		
		func2 = script.Parent.Frame.No.TextButton.MouseButton1Click:Connect(function()
			script.Parent.Visible = false
			sound:PlayLocalSound(script.Click)
                  func:Disconnect()
                  func2:Disconnect()
		end)

   

	end
end)
3 Likes

It works now thanks how does disconnect work though

1 Like

basically when you connect a function you basically tell the code that the function will fire when a certain event is fired, for ex:
if you connect a function which prints “hello” when the part is touched, you tell the code to fire this function when the part is touched, and disconnect just removes the connection between the function and the event, meaning that now nothing will happen if the part is touched.
in case of your code, you were connecting the function again each time but not disconnecting it, so the connections stacked and seemed like you were buying it multiple times

2 Likes

Ah, i see thanks for the help!

1 Like