Pairing System Issue

I am creating a pairing system. Whenever a player requests to pair, it fires a remote event to the player who triggered the proximity prompt and opens a GUI with two choices. When the player selects a choice, it fires it back to the server and functions accordingly. Though, when the player creates a pairing request and then it expires, and then the player requests another time, the other player receives the request one more time. For example, player 1 wants to pair up with player 2, but the request expires (deletes), then player 1 requests again, it creates 2 requests for some reason, then if those expire, it creates 3 the next time. I need help. Here is the relevant code:

if plr:WaitForChild("Paired").Value == "" then -- checks if the requested player isn't paired yet
	game.ReplicatedStorage.SendInfo:FireClient(plrWT, plr) -- fires to the player who triggered the prompt, and it sends the player who they want to pair up with through the event
end
game.ReplicatedStorage.SendInfo.OnClientEvent:Connect(function(plr) 
	script.Parent.Parent.Enabled = true -- GUI enables
	print('r!')
	for _, child in ipairs(script.Parent:GetChildren()) do -- loops through the children of the frame, the frame has 2 text button children, these are the possible choices that the player can choose
		if child:IsA("TextButton") then
			child.Activated:Connect(function()
				game.ReplicatedStorage.SendInfo:FireServer(plr, child.Name) -- if a choice is selected, it fires back to the server
				print("S!")
				script.Parent.Parent.Enabled = false -- turns on the gui again
				return
			end)
		end
	end
end)

Again, for some reason, the requests a player receives after their request expires keeps increasing by 1. Why?

By default, requests that aren’t accepted in 15 seconds expire. I feel like I am not exiting out of the function correctly.

I could be wrong but you don’t seem to be disconnecting the .Activated event, this would basically cause the event to be fired twice whenever the button is pressed, and so on the more requests the player receives, try saving all the connections in a table and disconnecting them afterwards, something like

local Connections = {}

game.ReplicatedStorage.SendInfo.OnClientEvent:Connect(function(plr) 
	if #Connections > 0 then
		for _, Connection in Connections do
			Connection:Disconnect()
		end
		Connections = {}
	end

	for _, child in ipairs(script.Parent:GetChildren()) do
		if child:IsA("TextButton") then
			-- add the connected event to a table
			table.insert(Connections, child.Activated:Connect(function()
				-- ur code
			end))
		end
	end
end)

I’ll try it out. Studio isn’t really working for me right now, but I will keep you updated. Thanks.

It works, thank you so much! I appreciate it a lot.

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