RemoteEvent Firing whenever a function gets called

I’m facing a problem: The amount of times I call my function is the amount of times it would call the remoteevent. Example - function is called once at the end of one round it would increment the value by 1, function is called once at the end of another round it would increment the value by 2.

I tried adding a debounce, re-arranging the code, and messing when the round system calls the function.

Help would be appreciated!

The function!


function vote()
		if Values.Playable.Value == false then
			Values.VotingTime.Value = true
				Remotes.enableVoting:FireAllClients()
					Remotes.Mapvote.OnServerEvent:Connect(function(plr,objClickedName)
					if Values.VotingTime.Value then
						print("Voted for map")
						onClientClick(objClickedName)
						Remotes.displayGamemodeGUI:FireClient(plr)
					end
				end)
				print("Step 7,8")
					Remotes.GamemodeVote.OnServerEvent:Connect(function(plr,objClickedName)
						if Values.VotingTime.Value then
							print("Voted for gamemode")
							onClientClickGamemode(objClickedName)
							Remotes.removeVotingGui:FireClient(plr)
						end
					end)
					for i = 10, 0,-1 do
						wait(1)
					if i < 1 then
						Values.VotingTime.Value = false
						break
					end
				end
			end 

this is still pretty unclear which function is doing what wrong

Alright, I’ll list more of the code here.

nono more isnt the problem i need less giving a million lines of code will just put people off trying to solve it just narrow it down a bit and give a clearer explanation like saving a value gets incremeted you need to show which value (but that isnt the main concern anyway)

You never disconnected the events (it keeps connecting everytime the function is called)

What should I do to disconnect them? (My first thought it to separate them from the vote function so It does not keep getting called)

You could do that, as that’s an easier solution, but for future reference:

local connection1
local connection2

function vote()
   if connection1 then
      connection1:Disconnect() -- disconnect the old events
   end

   if connection2 then
      connection2:Disconnect()
   end

		if Values.Playable.Value == false then
			Values.VotingTime.Value = true
				Remotes.enableVoting:FireAllClients()
				connection1 = Remotes.Mapvote.OnServerEvent:Connect(function(plr,objClickedName)
                    -- connect the variables to the events
					if Values.VotingTime.Value then
						print("Voted for map")
						onClientClick(objClickedName)
						Remotes.displayGamemodeGUI:FireClient(plr)
					end
				end)
				print("Step 7,8")
					connection2 = Remotes.GamemodeVote.OnServerEvent:Connect(function(plr,objClickedName)
						if Values.VotingTime.Value then
							print("Voted for gamemode")
							onClientClickGamemode(objClickedName)
							Remotes.removeVotingGui:FireClient(plr)
						end
					end)
					for i = 10, 0,-1 do
						wait(1)
					if i < 1 then
						Values.VotingTime.Value = false
						break
					end
				end
			end 

Ah! I see, Thanks. I’m so shocked I didn’t think of that, didnt know you can do that for forced remoteevent functions!