Connection is not disconnecting in coroutine.wrap

Hello everyone,
I’m writing a script where an NPC will be spawned. The Player can interact with it and give it cake, but when I give cake to the NPC, the connection will not be disconnected.

I give cake to one NPC, and it works.
I give cake to another NPC, the first NPC will register it too.
I give cake to the third NPC, and the first two NPCs will register it too.

Here’s the code:

coroutine.wrap(function()
		repeat wait() until OrderNumber == OrderQueue[1]

		ProximityPrompt.Enabled = true

		local connection : RBXScriptConnection
		connection = ProximityPrompt.Triggered:Connect(function(Cashier)
			moduleScript.DisplayBubble(NPC, "Hello, I would like the cake on "..cakeNumber..".")

			ProximityPrompt.Enabled = false

			task.spawn(function()
				wait(15)
				ProximityPrompt.Enabled = true
			end)

			for _, placement : BasePart in ipairs(Placements:GetChildren()) do
				local placementProximityPrompt: ProximityPrompt = placement:FindFirstChildWhichIsA("ProximityPrompt")
				placementProximityPrompt.Triggered:Connect(function(Player)
					if Player ~= Cashier then return end

					if placementProximityPrompt.Parent.Name == tostring(cakeNumber) then
						connection:Disconnect()
						print("right cake")
					else
						print("not this one")
					end
				end)
			end
		end)
	end)()

You could replace it with task.spawn

task.spawn(function()
	repeat wait() until OrderNumber == OrderQueue[1]

	ProximityPrompt.Enabled = true

	local connection : RBXScriptConnection
	connection = ProximityPrompt.Triggered:Connect(function(Cashier)
		moduleScript.DisplayBubble(NPC, "Hello, I would like the cake on "..cakeNumber..".")

		ProximityPrompt.Enabled = false

		task.spawn(function()
			wait(15)
			ProximityPrompt.Enabled = true
		end)

		for _, placement : BasePart in ipairs(Placements:GetChildren()) do
			local placementProximityPrompt: ProximityPrompt = placement:FindFirstChildWhichIsA("ProximityPrompt")
			placementProximityPrompt.Triggered:Connect(function(Player)
				if Player ~= Cashier then return end

				if placementProximityPrompt.Parent.Name == tostring(cakeNumber) then
					connection:Disconnect()
					print("right cake")
				else
					print("not this one")
				end
			end)
		end
	end)
end)

I’ve tried that as well. Now I have the idea of listing every placement connection into the table and disconnecting them all after I give the right cake. Wait a sec.

The problem was when I disconnected ProximityPrompt.Triggered:Connect(function(Cashier) … end) so NPC won’t talk anymore, but it will continue in registering placementProximityPrompt.Triggered:Connect(function(Player) … end) and that was the problem.
To solve it I made a table with connections and after I gave the right cake, every single connection got disconnected.

Are you sure this if statement is running? Does “right cake” print?

edit: ik this reply came after your one because we hit them literally at the exact same time so i didnt see yours before writing this one sorry

1 Like

Yes, it does.
Other NPCs had the placementProximity connections still connected, so they ran after I gave a cake to another NPC.

Edit: It’s alright. Thank you for your help.

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