Disconnecting a function from a remote event

  1. What do you want to achieve?
    I have a GUI which has a certain player pick 4 players to compete in a challenge, which works on the first loop, however on the second loop it will put a player chosen from the GUI twice and on the third loop three times and so on.

  2. What is the issue?
    I want the GUI to only send the player once to the main script and insert them into the “Elimination Players Table” once however after the first loop it will send a player multiple times.

  3. What solutions have you tried so far?
    I have tried disconnecting the remote Event after the first loop with the lines

local connection = voteEVENT.OnServerEvent:Connect(recievevote)
connection:Disconnect()

However, this doesn’t work for some reason and its placed at the bottom of my loop.
More code to help showcase what I was doing-
Serverscript-

EliminationPlayers = {}
				function recievevote(i, player)
					 	player = game.Players[player]
						table.insert(EliminationPlayers, player)
						if #EliminationPlayers == 4 then
							for i, v in pairs(thronePlayer) do
								voteguiEVENT:FireClient(v)
								end
						end
				end
				voteEVENT.OnServerEvent:Connect(recievevote)

Local Script in the GUI

function voteupdate(contestants)
    local avaliableBTNS = script.Parent.Frame:GetChildren()
    
    for i, player in pairs(contestants) do
        script.Parent.Enabled = true
        open()
        local button = avaliableBTNS[1]
        button.Visible = true
        button.Text = player.Name
     
		button.MouseButton1Down:Connect(function()
        voteEVENT:FireServer(button.Text)
        button.Visible = false
        end)
     

        table.remove(avaliableBTNS, 1)
    end    
end

function hideVote(v)
    close()
    script.Parent.Enabled = false
end

voteguiEVENT.OnClientEvent:Connect(hideVote)
voteEVENT.OnClientEvent:Connect(voteupdate)
1 Like

It’s because the button you click to fire the remote event can still be hit even though you removed it from the table (you didn’t remove it from the actual game). What you can do is disconnect the event inside itself when the event fires, so it’s a one-time only.

Or

You can use table.find to see if the player is already added to the elimination table:

if table.find(EliminationPlayers, player) == nil then --nil means it doesn't exist in the table, a number returned means that it does

    --code

end
2 Likes

Thank you for the help!

if table.find(EliminationPlayers, player) == nil then

Solved my issue quickly

2 Likes

you could just do this

  if not EliminationPlayers[player] then
   -- or even
  if not table.find(EliminationPlayers, player)

Just see if the index exists using direct-indexing