Hello! I’m trying to create a voting system and I’m currently working on my function that counts the votes and generates percentages for each candidate based on the total amount of votes submitted.
The problem is that although I have different values for each candidate, one of the candidates keep getting 100% which is false.
Here is my code and below it is the image of what I get as the final result
CODE
local function countBallots()
local pages = BalletStorage:GetSortedAsync(false, 100)
local data = pages:GetCurrentPage()
local totalVotes = 0
local livePercentage = {}
for i, v in pairs(data) do
local candidacyData = CandidateData:GetAsync(v.key)
ReplicatedStorage.Candidates[candidacyData.candidate].Value += 1
totalVotes += 1
print(ReplicatedStorage.Candidates[candidacyData.candidate].Name.." now has "..ReplicatedStorage.Candidates[candidacyData.candidate].Value.." votes!")
end
task.wait(1)
-- Generate percentages
for _, ballot in ReplicatedStorage.Candidates:GetChildren() do
local percentage
if ballot.Value == 0 then
percentage = 0
else
percentage = math.floor(math.floor((ballot.Value/totalVotes) + 0.5)*100)
end
print(percentage)
local ballotData = {
[ballot.Name] = percentage.."%"
}
table.insert(livePercentage, ballotData)
end
print(livePercentage)
end
RESULT
How can I fix this bug or error? I’m not getting any specific errors therefore the code is working but the math might be incorrect.