Voting count not working

I made a voting gui system thingy and it works fine… but only the first few times, after that it starts to have an incorrect number of votes and I don’t know why

I didnt see anything on the forums that helped me at all and I tried a few things but none of them worked. This is my first devforum post so if this is like Not good then sorry about that

The yesVotes and noVotes inside the script are NumberValues btw. The yesVote and noVote remotevents are fired by buttons in the gui that all players get when voting starts

local rs = game:GetService("ReplicatedStorage")
local events = rs.VotingRemoteEvents

events.StartVoting.Event:Connect(function()
	local yesVotes = script.yesVotes
	local noVotes = script.noVotes

	yesVotes.Value = 0
	noVotes.Value = 0
	
	for i, v in pairs(game:GetService("Players"):GetPlayers()) do
		if v.PlayerGui:FindFirstChild("VoteToSleep") then
			local gui = v.PlayerGui:FindFirstChild("VoteToSleep")
			gui.Frame.Votes.Text = "Votes: "..yesVotes.Value.."/"..noVotes.Value
		end
	end
	
	events.YesVote.OnServerEvent:Connect(function()
		yesVotes.Value += 1
		for i, v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.PlayerGui:FindFirstChild("VoteToSleep") then
				local gui = v.PlayerGui:FindFirstChild("VoteToSleep")
				gui.Frame.Votes.Text = "Votes: "..yesVotes.Value.."/"..noVotes.Value
			end
		end
	end)
	
	events.NoVote.OnServerEvent:Connect(function()
		noVotes.Value += 1
		for i, v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.PlayerGui:FindFirstChild("VoteToSleep") then
				local gui = v.PlayerGui:FindFirstChild("VoteToSleep")
				gui.Frame.Votes.Text = "Votes: "..yesVotes.Value.."/"..noVotes.Value
			end
		end
	end)
	
	local timer = 15
	for i = 1, 15 do
		wait(1)
		timer -= 1
		for i, v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.PlayerGui:FindFirstChild("VoteToSleep") then
				local gui = v.PlayerGui:FindFirstChild("VoteToSleep")
				gui.Frame.Timer.Text = "Voting Time: "..timer
			end
		end
		local players = 0
		for i, v in pairs(game:GetService("Players"):GetPlayers()) do
			players += 1
		end
		if noVotes.Value + yesVotes.Value >= players then
			break
		end
	end
	
	if yesVotes.Value > noVotes.Value then
		for i, v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.PlayerGui:FindFirstChild("VoteToSleep") then
				local gui = v.PlayerGui:FindFirstChild("VoteToSleep")
				gui:Destroy()
			end
		end
		events.Sleep:Fire()
		yesVotes.Value = 0
		noVotes.Value = 0
	else
		for i, v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.PlayerGui:FindFirstChild("VoteToSleep") then
				local gui = v.PlayerGui:FindFirstChild("VoteToSleep")
				gui:Destroy()
			end
		end
		yesVotes.Value = 0
		noVotes.Value = 0
	end
end)

why are you doing this? You don’t need to decrease, just use i instead. Additionally, wait(number) is deprecated, use task.wait(number) instead.

The timer variable is what shows on the gui, The timer decreases but if i used i it would increase. Also there’s no reason not to use wait it’s easier to type and works the same

Generally you should try to not modify PlayerGui’s from server scripts.
You can have values in ReplicatedStorage that the client GUI scripts can watch with the OnChange.

The reason why it keeps incrementing is because it looks like you’re setting up new connections for the yes/no votes each time voting starts.
events.NoVote.OnServerEvent:Connect(function()...

p.s. Also in this case you’re right that you probably don’t notice a difference between task.wait and wait but they are different and do not work the same.

You are wrong, wait() shouldn’t be used at any cost.

Regarding the timer you can just do this:

for i = 15, 0, -1 do
1 Like

You have an event connection in an event connection. So every time StartVoting event is fired there are two more event connections created. By the time you’ve hit the sleep button 5 times, which fires the startVoting event, you now have 10 connections going on, each adding onto the value and causing all sorts of wackiness.

You need to place the YesVote and NoVote events outside of the StartVote event

2 Likes

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