Hello, I am currently making a voting system and it gets the highest number that is voted, however, that is nil. Can anyone tell me why? This is a server script in SSS.
local padVotes = {
["Bedroom"] = game.ReplicatedStorage.Votes.Bedroom.Value,
["MrBeast"] = game.ReplicatedStorage.Votes.Mrbeast.Value,
}
for i, v in pairs(padVotes) do
local number = v
if number > highestNumber then
highestNumber = number
chosen = tostring(i)
end
end
local ChosenMap = tostring(chosen)
print(chosen)
print(ChosenMap)
task.wait(1)
if ChosenMap == nil or ChosenMap == "0" or ChosenMap == "nil" then
StatusMap.Value = "no map has been chosen!"
local AvailableMaps = Maps:GetChildren()
ChosenMap = tostring(AvailableMaps[math.random(1,#AvailableMaps)])
task.wait(2)
StatusMap.Value = "Map voted: " ..ChosenMap.. "!"
else
StatusMap.Value = "Map voted: " ..ChosenMap.. "!"
end
task.wait(1)
Status.Value = "Game starting!"
Can you print(number) and print(highestNumber) inside the for loop?
Print the highestnumber after and before the for loop as well, just to see where the issue comes from
This only records the vote count at the moment the script starts running, unless this code is part of a function that you haven’t shown. This is probably not what you wanted.
The “highest” check is almost right but you’re missing the case where everything has the same number of votes:
local chosen = nil
for i, v in pairs(padVotes) do
local padValue = v.Value
if chosen == nil or padValue > highestNumber then
highestNumber = padValue
chosen = tostring(i)
end
end
Pretty sure it’s nil because the votes for every map is 0. In this line, you’re checking if the map votes is the highest number. In this case, it’s checking for 0 > 0 (as evident in your output), which is inputting as false since number is not more than highestNumber.
Try using the >= operator instead and see if there’s any different results:
Partially works. When i try to vote “Mrbeast” map, it still votes as bedroom and prints Bedroom as chosen and ChosenMap variables instead of nil, so i guess thats progress! .
It’s nil because you’re only storing the values in padVotes, you want to store the Bedroom, and Mrbeast.
since what you’re storing is the value nil, all you have to convert to a string when they are chosen is nil.
If you say did this instead:
local padVotes = {
["Bedroom"] = game.ReplicatedStorage.Votes.Bedroom,
["MrBeast"] = game.ReplicatedStorage.Votes.Mrbeast,
}
-- I removed .Value at the end
for i, v in pairs(padVotes) do
local number = v.Value
if number > highestNumber then
highestNumber = number
-- as mentioned you're not accounting for ties, you should think of a system to account for ties, maybe write a few functions to account for these scenarios.
else if number == highestNumber then
-- code in case of tie
end
end
Also just use the value of i since it should already be a string.
Is there a chance we could see the explorer for the items you’re trying to call? It might help with debugging if we knew what you’re trying to work with.
It just dawned on me, but I’m not seeing any code that manages waiting for player votes. from what I can see it just runs the second the server starts.