Value inside of if statement coming out as nil

The issue is the variable picked inside of if amountOfMaps comes out as nil when referenced outside of the script but all of the other variables picked come out as they should. I am very lost and would appreciate any help. Thank you.

if  amountOfMaps >= 1 then
		
		local picked = mapsInQueueCheck[math.random(1, #mapsInQueueCheck)]
		
		local playerPurchased = picked.Value
		
		local name = picked.Name
		
		print(playerPurchased)
		print(picked)
		
		status.Value = playerPurchased.." picked "..name.."!"
		wait(5)
	end
	
	if amountOfMaps == 0 then

		
		local  mostVotes = 0

		for i, v in pairs(votedMaps) do
			local number = tonumber(v)
			if number > mostVotes then
				mostVotes = number
			end
		end
	
		if mostVotes == 0 then 
		
			status.Value = "No maps Voted!"
			wait(5)
		
			picked = mapVotingOptions[math.random(1, #mapVotingOptions)]

			status.Value = "Random Map: "..picked
		
			else if mostVotes > 0 then
		
		if voteMapOne.Value == mostVotes then

			picked = table.concat(mapVotingOptions,'',1,1)

			status.Value = picked.." had the most votes!"

		end

		if voteMapTwo.Value == mostVotes then

			picked = table.concat(mapVotingOptions,'',2,2)

			status.Value = picked.." had the most votes!"

		end

		if voteMapThree.Value == mostVotes then

			picked = table.concat(mapVotingOptions,'',3,3)

					status.Value = picked.." had the most votes!"
					
				end
			end
		end
	end
	
	game.ReplicatedStorage.MapEvents.votingTriggers.closeVoting:FireAllClients()
	
	print(picked)
	
	local randMap = Maps:FindFirstChild(picked)

output

local picked
if  amountOfMaps >= 1 then
    picked = mapsInQueueCheck[math.random(1, #mapsInQueueCheck)]
    
    local playerPurchased = picked.Value
    
    local name = picked.Name
    status.Value = playerPurchased.." picked "..name.."!"
elseif amountOfMaps == 0 then
    local mostVotes = 0
    for i, v in pairs(votedMaps) do
        local number = tonumber(v)
        if number > mostVotes then
            mostVotes = number
        end
    end
    if mostVotes == 0 then 
        status.Value = "No maps Voted!"
        picked = mapVotingOptions[math.random(1, #mapVotingOptions)]
        status.Value = "Random Map: "..picked
    else
        if voteMapOne.Value == mostVotes then
            picked = table.concat(mapVotingOptions,'',1,1)
            status.Value = picked.." had the most votes!"
        elseif voteMapTwo.Value == mostVotes then
            picked = table.concat(mapVotingOptions,'',2,2)
            status.Value = picked.." had the most votes!"
        elseif voteMapThree.Value == mostVotes then
            picked = table.concat(mapVotingOptions,'',3,3)
            status.Value = picked.." had the most votes!"
        end
    end
end
task.wait(5)
game.ReplicatedStorage.MapEvents.votingTriggers.closeVoting:FireAllClients()

print(picked)

local randMap = Maps:FindFirstChild(picked)

I tried to clean up your code a little. Does this function as you want? It was hard to understand what you meant when a code segment and screenshot out of context. I changed the picked variable to be defined outside of the if statements so it can be used globally.

3 Likes

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