Trouble with ProximityPrompt firing multiple times upon being triggered

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would like some help seeing what is wrong with my current code making a ProximityPrompt fire twice.

  2. What is the issue? Include screenshots / videos if possible!
    Upon firing the prompt the first time it works just fine, but once I fire it a second it creates 2 clones, 3 for the third trigger and so forth

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried fully rewriting the code but that didn’t fix anything. Neither did adding a debounce like another person’s topic suggested.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!


local doughMaker = script.Parent
local promptPart = doughMaker.PromptPart

local building1 = doughMaker.Parent
local bakery1 = building1.Parent
local owner = bakery1.Owner

local ingredientsFolderSS = ServerStorage:WaitForChild("Ingredients")
local toolsFolder = ServerStorage:WaitForChild("Tools")
local ingredientTools = toolsFolder:WaitForChild("Ingredients")

local DB = false

doughMaker.PromptPart.ProximityPrompt.Triggered:Connect(function(player)
	if DB == false then
		DB = true
		if player.Name == owner.Value then

			local mainGUI = player.PlayerGui:WaitForChild("MainGui")
			local doughGUI = mainGUI:WaitForChild("DoughMenu")
			local leftSideButtons = mainGUI:WaitForChild("LeftSideButtons")
			local closeButton = doughGUI:WaitForChild("Close")

			leftSideButtons.Visible = false
			doughGUI.Visible = true

			for i, v in pairs(doughGUI.Buttons:GetChildren()) do
				if v:IsA("Frame") then
					if building1.Recipes:FindFirstChild(v.Name) then
						v.Visible = true

						v.Button.MouseButton1Click:Connect(function()
							doughMaker.PromptPart.ProximityPrompt.Enabled = false
							doughGUI.Visible = false
							local newOBJ = ingredientsFolderSS:FindFirstChild(v.Name)["1"]:Clone()
							newOBJ.Name = "OBJ"
							newOBJ.Position = doughMaker.Bottom.Position - Vector3.new(0,0.5,0)

							local newPrompt = Instance.new("ProximityPrompt")
							newPrompt.ObjectText = "Pick up"
							newPrompt.ActionText = "Dough"
							newPrompt.HoldDuration = 1
							newPrompt.Parent = newOBJ
							
							print("fired")
							
							newOBJ.Parent = doughMaker
							
							leftSideButtons.Visible = true

							newPrompt.Triggered:Connect(function(player2)
								if player2.Name == owner.Value then
									local newTool = ingredientTools:FindFirstChild(v.Name.." dough"):Clone()
									newTool.Parent = player.Backpack
									doughMaker.PromptPart.ProximityPrompt.Enabled = true
									newOBJ:Destroy()
								end
							end)
						end)
					end
				end
			end

			closeButton.MouseButton1Click:Connect(function()
				doughGUI.Visible = false
			end)
		end
		wait(.25)
		DB = false
	end
end)
2 Likes

By an chance is the 2 clones the variable newOBJ?

If so, the issue is stemming from the fact you every time you trigger the prox prompt you’re not disconnecting the functions inside the prox prompt function.

To remedy this issue assign a variable to each nested connect method and however you terminate this function ensure you disconnect each connection using the

:Disconnect()

method.

Let me know if this helps.

2 Likes

Hey, thank you for the reply and trying to help me!
Unfortunately I don’t understand how the disconnect function works, so I tried looking it up and just doing some stuff with it but I couldn’t get anything to work.

Also what do you mean by “Assign a variable to each nested connect method”?
Because I think I might be doing something wrong there.

Everytime you use the :Connect() method to an event think that you are creating an instance of a part.

So everytime this code runs you have a connection to that “doughMaker”.

The initial connect function you provided (which is above) should only run once, but the proximityprompt can be triggered multiple times. So if there are any :Connect() methods inside of the doughMaker connected function and you trigger that proximity prompt multiple times you will instantiate new connections which will trigger that same code multiple times.

Assigning a variable to the connection will allow you to disconnect whenever said player close the DoughGui or whatever you use to allow the player to stop using that feature.

Example

doughMaker.PromptPart.ProximityPrompt.Triggered:Connect(function(player)
     local buttonClickConnection = nil
     local newPromptConnection = nil
     local closeButtonConnection = nil

     buttonClickConnection = v.Button.MouseButton1Click:Connect(function()
          -- your click button code

          newPromptConnection = newPrompt.Triggered:Connect(function(player2)
               -- your newPrompt code
          end)
     end)
    closeButtonConnection =  closeButton.MouseButton1Click:Connect(function() 
          -- your close button code

          buttonClickConnection:Disconnect()
          newPromptConnection:Disconnect()
          closeButtonConnection:Disconnect()
     end)
end)

I’ve tried doing it how you showed me, again thank you for that, I think I understand it a little bit better now, but it didn’t change anything, it still keeps adding 1 more every time the prompt is triggered. Again, thank you for trying to help me though!

try a debounce in

Clicks I would say is a bit more fincky than prox prompts

1 Like

Thank you so very much! It worked!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.