Attempt to index nil with parent

Alright, so I am working on a map voting system for a game, part of it of course involves parenting the map.

Please note this is a 300+ line script, Ima just show the portions of the scripts that is envolved

		local number1 = math.random(1,#Maps)
		local number2 = math.random(1,#Maps)
		if number1 == number2 then
			repeat
				number2 = math.random(1,#Maps)
				wait()
			until number2 ~= number1 
		end
		
	--[[	local number3 = math.random(1,#Maps)
		
		if number3 == number2 or number3 == number1 then
			repeat
				number3 = math.random(1,#Maps)
				wait()
			until number3 ~= number2 and number3 ~= number1 
		end]]
		
		
		local map1 = Maps[number1].Name
		local map2 = Maps[number2].Name
		--local map3 = Maps[number3]
		
		VotingEvent:FireAllClients(map1, map2)
		
		for i=20,0,-1 do
			print(#currentvotes)
			if #currentvotes == #GetPlayers() then
				break
			end
			DisplayEvent:FireAllClients("Voting : "..i)
			wait(1)
		end
		
		local map1votes = {}
		local map2votes = {}
		local map3votes = {}
		
		for i,v in pairs(currentvotes) do
			if v == map1 then
				table.insert(map1votes, v)
			elseif v == map2 then
				table.insert(map2votes, v)
		--	elseif v == map3 then
				--table.insert(map3votes, v)
			end
		end
		
		for i=1,#currentvotes do
			table.remove(currentvotes, i)
		end
		
		local max = math.max(#map1votes, #map2votes)
		
		if max == #map1votes then
			if max == #map2votes then
				local map = Maps[math.random(1,#Maps)] -- Picking random map
				local newmap = map:Clone()
				print('tie', newmap.Name)
			elseif max ~= #map2votes then
				newmap = Maps[number1]:Clone()
			end
		elseif max == #map2votes then
			if max == #map1votes then
				local map = Maps[math.random(1,#Maps)] -- Picking random map
				local newmap = map:Clone()
				print('tie', newmap.Name)
			elseif max ~= #map1votes then
				newmap = Maps[number2]:Clone()
			end
		end
		
		
		newmap.Parent = MapHolder

Commented out portions are for a third map later in time, we only have 2 at the moment…

The voting system works perfectly until there is a tie, once there is a tie it decides to throw an error, “Attempt to index nil with parent”

I have prints through out the script as you can see, they are all printing the right things such as number of votes, maps voted, and its defining the map after the tie, but for some reason it does not want to parent it…

Archivable on the map is set to true as well.

Any help is appreciated

I’m going to talk about this block of code, I will highlight issues with comments:

if max == #map1votes then
	if max == #map2votes then
		local map = Maps[math.random(1,#Maps)]
		local newmap = map:Clone() -- Issue
		print('tie', newmap.Name)
	elseif max ~= #map2votes then
		newmap = Maps[number1]:Clone()
	end
elseif max == #map2votes then
	if max == #map1votes then
		local map = Maps[math.random(1,#Maps)] -- Picking random map
		local newmap = map:Clone() -- Issue
		print('tie', newmap.Name)
	elseif max ~= #map1votes then
		newmap = Maps[number2]:Clone()
	end
end

The newmap is not defined in the same scope as where newmap.Parent = MapHolder is. To fix this problem, move the variable to the same scope:

local newmap -- new variable
if max == #map1votes then
	if max == #map2votes then
		local map = Maps[math.random(1,#Maps)]
		newmap = map:Clone()
		print('tie', newmap.Name)
	elseif max ~= #map2votes then
		newmap = Maps[number1]:Clone()
	end
elseif max == #map2votes then
	if max == #map1votes then
		local map = Maps[math.random(1,#Maps)] -- Picking random map
		newmap = map:Clone()
		print('tie', newmap.Name)
	elseif max ~= #map1votes then
		newmap = Maps[number2]:Clone()
	end
end

This should resolve your issue.

1 Like

Thank you, I had it defined above but I didn’t realize I put a local before renaming it, that would explain why it works without the tie, I appreciate it!