Why is this nil?

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!"

And in output:
image

from print(chosen) and print(ChosenMap)

also I tried printing in the for loop:

1 Like

Make a empty variable called chosen at the top of the script

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

Does it not get changed?

local padVotes = {
	["Bedroom"] = game.ReplicatedStorage.Votes.Bedroom.Value,
	["MrBeast"] = game.ReplicatedStorage.Votes.Mrbeast.Value,
}

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

oh, probably thats the reason why then. How would i fix this?

image

I did this, number is line 60, highestnumber is outside the loop, i did one inside the loop but it didnt print.

Store the Value instance (the IntValue), then get the .Value at the time you want to check it. See my edit.


I already checked if none has been voted.

How would I do this??? It runs after the voting too so idk.

local padVotes = {
	["Bedroom"] = game.ReplicatedStorage.Votes.Bedroom,
	["MrBeast"] = game.ReplicatedStorage.Votes.Mrbeast,
}

I tried doing this then added v.Value in the for loop, but no different results :frowning:

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:

if number >= highestNumber then
1 Like

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! :smiley: .

1 Like

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.

1 Like

It accounts for a tie now;
image
But I chose bedroom and it prints nil for ChosenMap…

Have you tried debugging how many votes each map has or checking that the NumberValues have the correct values in the Explorer tab?

If the number variable is printing as 0, it could be that your vote isn’t being added to the value for whatever reason.

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 is being added. I made sure of that when I first created this topic;

also @LyokoMarty10101 heres the explorer you asked for where its being called on

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.