Why is my vote command unreliable?

For the judges in my game, I’m having them use a command to vote for a player. It works, but sometimes not until the 2nd or 3rd time saying the command

local enabled = false
game.ReplicatedStorage.Vote.OnClientEvent:Connect(function()
	enabled = true
	game.StarterGui:SetCore("SendNotification",{
		Title = "!!HOW TO VOTE!!",
		Text = "Say vote1 for player 1, and say vote2 for player 2!",
		Time = 20
	})
	
	if enabled == true then 
		local player = game.Players.LocalPlayer

		player.Chatted:Connect(function(msg)
			print("1")
			if msg:lower():sub(1,5) == 'vote1' and enabled == true then
				print("2")
				enabled = false
				wait(.1)
				local point = 1
				wait(.1)
				game.ReplicatedStorage.CalVotes:FireServer(point)
				game.StarterGui:SetCore("SendNotification",{
					Title = "Success!",
					Text = "You have successfully voted for Player 1!",
					Time = 5
				})
			elseif msg:lower():sub(1,5) == 'vote2' and enabled == true then
				print("3")
				enabled = false
				wait(.1)
				local point = 2
				wait(.1)
				game.ReplicatedStorage.CalVotes:FireServer(point)
				game.StarterGui:SetCore("SendNotification",{
					Title = "Success!",
					Text = "You have successfully voted for Player 2!",
					Time = 5
				})
			end
		end)
	end
end)

game.ReplicatedStorage.EndVote.OnClientEvent:Connect(function()
	enabled = false
end)

So your problem is, it stops working after the 2nd or 3rd time using it? Where is the code where you are calling the game.ReplicatedStorage.EndVote remote?

No, I mean the code won’t actually work until you say the command for the 2nd or 3rd try. Almost every time the first try with the command won’t work

How’s that? Also what’s the point of the variable

local enabled = false

When in the game.Replicated.Vote.OnClientEvent function you’re enabling it before you even do the sanity check?

Anyway, by any chance do you have the code where you are calling the game.ReplicatedStorage.EndVote remote?

Instead of having the .Chatted function embedded inside the on client event, instead have it outside of it and whenever a player chats, check if enabled equals to true and if so then continue on from there.

1 Like

I was using it to make sure they can’t vote more than once during the voting time, that’s the best way I’d know how to do it

In the server script it will only fire the RemoteEvent to the players on the correct team, would I still need sanity check in the local script?

This is my script:

function handleVoting(player1, player2)
	local timer = 20
	screenText = "Judges voting... (" .. timer .. ")"	
	wait(.1)
	Remote:FireAllClients(screenText)
	wait(1)
	local judgeTeam = game.Teams.Judges:GetPlayers()
	for i, player in pairs(judgeTeam) do
		game.ReplicatedStorage.Vote:FireClient(player)
	end
	
	repeat
		timer = timer - 1
		wait(.01)
		screenText = "Judges voting... (" .. timer .. ")"
		wait(.01)
		Remote:FireAllClients(screenText)
		wait(1)
	until
	timer == 0
	
	game.ReplicatedStorage.EndVote:FireAllClients()
	
	if Player1Value.Value > Player2Value.Value then
		screenText = player2.Name .. " has been eliminated!"
		wait(.1)
		Remote:FireAllClients(screenText)
		wait(3)
		if player2 and player2.Character then
			player2.Team = lobbyTeam
			player2.Character:MoveTo(lobbyTeleport.Position)
		end
		if player1 and player1.Character then
			player1.Character:MoveTo(lobbyTeleport.Position)
		end
		Player1Value.Value = 0
		Player2Value.Value = 0
		print("2")
		startRound()
		print("3")

	elseif Player2Value.Value > Player1Value.Value then
		screenText = player1.Name .. " has been eliminated!"
		wait(.1)
		Remote:FireAllClients(screenText)
		wait(3)
		if player2 and player2.Character then
			player2.Character:MoveTo(lobbyTeleport.Position)
		end
		if player1 and player1.Character then
			player1.Team = lobbyTeam
			player1.Character:MoveTo(lobbyTeleport.Position)
		end
		Player1Value.Value = 0
		Player2Value.Value = 0
		startRound()
	else
		screenText = "tie!"
		wait(.1)
		Remote:FireAllClients(screenText)
        --- have contestants vote
	end
end