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)