I’m trying to make a round system for my game, but I’m currently having issues with programming it. After I use table.sort
to sort out the items in the table, for some reason after I try to get the map that has the most votes, it prints out as nil and throws an error.
18:04:24.590 Roblox HQ - Server - Game:77
18:04:45.047 ▶ {...} - Server - Game:109
18:04:45.047 nil - Server - Game:110
18:04:45.047 31 - Server - Game:111
18:04:45.048 ServerScriptService.Game:123: attempt to index nil with 'Parent'
Here’s the code
while task.wait() do
if #Players:GetPlayers() >= 1 then
for i = 1, 4, 1 do
statusValue.Value = "◀ ".."Preparing Game.".." ▶"
task.wait(.5)
statusValue.Value = "◀ ".."Preparing Game..".." ▶"
task.wait(.5)
statusValue.Value = "◀ ".."Preparing Game...".." ▶"
task.wait(.5)
end
local option1 = maps:GetChildren()[math.random(1, #maps:GetChildren())]
print(option1)
local option2
repeat
option2 = maps:GetChildren()[math.random(1, #maps:GetChildren())]
until option2 ~= option1
print(option2)
local option3
repeat
option3 = maps:GetChildren()[math.random(1, #maps:GetChildren())]
until option3 ~= option1 and option3 ~= option2
print(option3)
local votes = {option1Votes = 0, option2Votes = 0, option3Votes = 0}
mapVotingRemote:FireAllClients(option1, option2, option3)
for i = 1, 20, 1 do
timeValue.Value = convertToHMS(20 - i)
task.wait(1)
end
task.spawn(function()
mapVotingRemote.OnServerEvent:Connect(function(player, voteOption)
if voteOption == 1 then
votes.option1Votes += 1
elseif voteOption == 2 then
votes.option2Votes += 1
elseif voteOption == 3 then
votes.option3Votes += 1
end
end)
end)
table.sort(votes, function(a, b)
return a > b
end)
local map
print(votes)
print(votes[1])
print(votes.option1Votes)
if votes.option1Votes == votes[1] then
map = option1:Clone()
print(map)
elseif votes.option2Votes == votes[1] then
map = option2:Clone()
print(map)
elseif votes.option3Votes == votes[1] then
map = option3:Clone()
print(map)
end
map.Parent = workspace
local gamemode = gamemodes[math.random(1, #gamemodes)]
local playerMode
if gamemode == gamemodes.Demolition then
playerMode = gamemodes.Demolition[math.random(1, #gamemodes.Demolition)]
if playerMode == "FFA" then
map:FindFirstChild("Spawns").BlueSpawns:Destroy()
map:FindFirstChild("Spawns").RedSpawns:Destroy()
end
end
statusValue.Value = "Map Selected, Beginning Game!"
task.wait(3)
screenTransitionRemote:FireAllClients(5)
end
end