I want to achieve to load a map when voted on it. Everything works fine but it errors on line 73 when I don’t pick a map.
This is the part where it errors:
function round.LoadMap()
local votedMap = round.ToggleVoting()
local mapFolder = ServerStorage.Maps:FindFirstChild(votedMap)
if not mapFolder then
mapFolder = ServerStorage.Maps.WheatFields
end
local newMap = mapFolder:Clone()
newMap.Parent = workspace.Map
Not sure if it has to do something with FindFirstChild()
function round.ToggleVoting()
local maps = ServerStorage.Maps:GetChildren()
votes = {}
for i, map in ipairs(maps) do
votes[map.Name] = {}
end
info.Voting.Value = true
for i=10, 0, -1 do
info.VotingTime.Value = "Map Voting... " .. i
task.wait(1)
end
local winVote = nil
local winScore = 0
for name, map in pairs(votes) do
if #map > winScore then
winScore = #map
winVote = name
end
end
info.Voting.Value = false
return winVote
end
From what I see, Each key value pair just has an empty table, nothing is added to the tables of each key, therefore #map is 0, so the condition is never met because 0 is not greater than 0, so winVote is nil,
I think it may be an issue with ToggleVoting itself, what were you trying to do?
votedMap returns nil if all the options are tied at 0 votes, and you cannot do :FindFirstChild() with a nil value, so check if it’s not nil before trying to pass it as an arg
function round.LoadMap()
local votedMap = round.ToggleVoting()
local mapFolder = ServerStorage.Maps.WheatFields
if votedMap then
mapFolder = ServerStorage.Maps:FindFirstChild(votedMap)
else
local newMap = mapFolder:Clone()
newMap.Parent = workspace.Map