Attempt to index a nil value

This 'end' is both dangling and 'extra':
Code:
local religionID = JFD_GetStateReligion(otherPlayerID) [COLOR="Red"]end[/COLOR]
These kinds of extra ends will cause the sort of error you are getting because it is the closest error lua can generate to the "real" problem.

Didn't get a chance to take a look at the rest of it. Gotta go for a while. Might be able to take a closer look through the rest of the code this evening if you are still having troubles.
 
Thanks, Lee! I found another issue similar to that later in the code which also caused the same error and have been able to eliminate it!

Going back to the original error I started the thread with, I thought I'd applied that but I hadn't. The solution given by Iamblichos worked, thank-you, but another small issue occurs later in the code. It seems that there's something I need to delete but I'm not quite sure what. Any help would be appreciated!

The highlighted line - which is only a ")" half way down - generates the syntax error: unexpected symbol near ')'

Code:
local Decisions_SacrificeTheMightiest = {}
	Decisions_SacrificeTheMightiest.Name = "TXT_KEY_DECISIONS_MASSAGETAE_SACRIFICE_THE_MIGHTIEST"
	Decisions_SacrificeTheMightiest.Desc = "TXT_KEY_DECISIONS_MASSAGETAE_SACRIFICE_THE_MIGHTIEST_DESC"
	HookDecisionCivilizationIcon(Decisions_SacrificeTheMightiest, "CIVILIZATION_MASSAGETAE")
	Decisions_SacrificeTheMightiest.CanFunc = (
	function(pPlayer)
		if (pPlayer:GetCivilizationType() ~= GameInfoTypes["CIVILIZATION_MASSAGETAE"]) then return false, false end
			if pPlayer:GetCapitalCity() then 
			local era = load(pPlayer, "Decisions_SacrificeTheMightiest")
			local currentEra = pPlayer:GetCurrentEra()
			if era ~= nil then
				if era < currentEra then
					save(pPlayer, "Decisions_SacrificeTheMightiest", nil)
				else
					Decisions_SacrificeTheMightiest.Desc = Locale.ConvertTextKey("TXT_KEY_DECISIONS_MASSAGETAE_SACRIFICE_THE_MIGHTIEST_ENACTED")
					return false, false, true
				end
			end
			
			local unitLevel = 0
			local hasUnit = false
			local plot = pPlayer:GetCapitalCity():Plot()
			for iVal = 0,(plot:GetNumUnits() - 1) do
				local unit = plot:GetUnit(iVal)
				if unit:GetUnitType() == GameInfoTypes["UNIT_GREAT_GENERAL"] then
					unitLevel = 5
					hasUnit = true
					break
				elseif unit and unit:GetUnitCombatType() == GameInfoTypes["UNITCOMBAT_MOUNTED"] then
					unitLevel = unit:GetLevel()
					if unitLevel > 5 then
						unitLevel = 5
					end
					hasUnit = true
					break
				end
			end

			local iFaith = pPlayer:GetFaithPerTurnFromCities() * unitLevel 
			local iCulture = pPlayer:GetJONSCulturePerTurnFromCities() * unitLevel
			Decisions_SacrificeTheMightiest.Desc = Locale.ConvertTextKey("TXT_KEY_DECISIONS_MASSAGETAE_SACRIFICE_THE_MIGHTIEST_DESC", iFaith, iCulture)

			if hasUnit == true then
				return true, true
			else
				return true, false
			end
	end
	[COLOR="Red"])[/COLOR]
	
	Decisions_SacrificeTheMightiest.DoFunc = (
	function(pPlayer)
		local unitLevel = 0
		local plot = pPlayer:GetCapitalCity():Plot()
		local killUnit
		for iVal = 0,(plot:GetNumUnits() - 1) do
			local unit = plot:GetUnit(iVal)
			if unit:GetUnitType() == GameInfoTypes["UNIT_GREAT_GENERAL"] then
				unitLevel = 5
				killUnit = unit
				break
			elseif unit and unit:GetUnitCombatType() == GameInfoTypes["UNITCOMBAT_MOUNTED"] then
				unitLevel = unit:GetLevel()
				if unitLevel > 5 then
					unitLevel = 5
				end
				killUnit = unit
				break
			end
			
		end

		local iFaith = pPlayer:GetFaithPerTurnFromCities() * unitLevel
		local iCulture = pPlayer:GetJONSCulturePerTurnFromCities() * unitLevel

		pPlayer:ChangeFaith(iFaith)
		pPlayer:ChangeJONSCulture(iCulture)

		for loopPlot in PlotAreaSweepIterator(plot, 1, SECTOR_NORTH, DIRECTION_CLOCKWISE, DIRECTION_OUTWARDS, CENTRE_INCLUDE) do
			local otherUnit = loopPlot:GetUnit()
			if otherUnit then
				if unitLevel == 1 then
					otherUnit:ChangeExperience(5)
				elseif unitLevel == 2 then
					otherUnit:ChangeExperience(10)
				elseif unitLevel == 3 then
					otherUnit:ChangeExperience(15)
				elseif unitLevel == 4 then
					otherUnit:ChangeExperience(20)
				elseif unitLevel == 5 then
					otherUnit:ChangeExperience(25)
				end
			end
		end;
		killUnit:Kill(true)
		save(pPlayer, "Decisions_SacrificeTheMightiest", pPlayer:GetCurrentEra())
	end
	)
	
Decisions_AddCivilisationSpecific(GameInfoTypes["CIVILIZATION_MASSAGETAE"], "Decisions_SacrificeTheMightiest", Decisions_SacrificeTheMightiest)
 
Thank-you, calcul8or!! That has worked, I'm really pleased. One last one to go now (or so I believe)! This one is giving the following error with the line highlighted in red: attempt to index local 'startingPlot' (a nil value). Hopefully one of you will be able to help me with this one also. I'd be extremely grateful for any assistance.

Code:
-- Tartessos functions - adapted from JFD_NorwayFunctions
-- Author: JFD
-- DateCreated: 6/23/2014 2:36:23 PM
--=======================================================================================================================
-- UTILITY FUNCTIONS	
--=======================================================================================================================
-- JFD_IsCivilisationActive
------------------------------------------------------------------------------------------------------------------------
function JFD_IsCivilisationActive(civilisationID)
	for iSlot = 0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
		local slotStatus = PreGame.GetSlotStatus(iSlot)
		if (slotStatus == SlotStatus["SS_TAKEN"] or slotStatus == SlotStatus["SS_COMPUTER"]) then
			if PreGame.GetCivilization(iSlot) == civilisationID then
				return true
			end
		end
	end

	return false
end
--==========================================================================================================================
-- CORE FUNCTIONS
--==========================================================================================================================
-- Globals
----------------------------------------------------------------------------------------------------------------------------
local civilisationID	= GameInfoTypes["CIVILIZATION_TARTESSOS"]
local isNorwayCivActive = JFD_IsCivilisationActive(civilisationID)
local isNorwayActivePlayer = Players[Game.GetActivePlayer()]:GetCivilizationType() == civilisationID
local mathMin			= math.min

if isNorwayCivActive then
	print("Arganthonios is in this game")
end
----------------------------------------------------------------------------------------------------------------------------
--InitArganthonios
-------------------------------------------------------------------------------------------------------------------------
local terrainCoastID = GameInfoTypes["TERRAIN_COAST"]
local worldHugeID = GameInfoTypes["WORLDSIZE_HUGE"]

function JFD_RevealCoast(playerID)
	local startingPlot = Players[playerID]:GetStartingPlot()
	local teamID = Players[playerID]:GetTeam()
	local distance = 20
	local mapSize = Map.GetWorldSize()
	if mapSize >= worldHugeID then distance = 30 end

	for i = 0, Map.GetNumPlots() - 1, 1 do
		local plot = Map.GetPlotByIndex(i)
		local plotX = plot:GetX()
		local plotY = plot:GetY()
		[COLOR="Red"]local startingPlotX = startingPlot:GetX()[/COLOR]
		local startingPlotY = startingPlot:GetY()
		if (plot:GetTerrainType() == terrainCoastID and Map.PlotDistance(startingPlotX, startingPlotY, plotX, plotY) <= distance) then
			plot:SetRevealed(teamID, true)
		end
	end
end

function JFD_InitArganthonios(player)
	for playerID = 0, GameDefines.MAX_MAJOR_CIVS - 1 do
		local player = Players[playerID]
		if player:GetCivilizationType() == civilisationID then
			JFD_RevealCoast(playerID)
		end
	end 
end

if isNorwayCivActive then
	Events.LoadScreenClose.Add(JFD_InitArganthonios)
end
 
I added suspenders and belts again, but I think your problem may be similar to the other one (upthread? / different thread?) where the iteration through all Major Players can attempt processing for civs that aren't even in the game, or in the case of Anno perhaps for civs that are occupying "two spaces", one in the actual game and one in the slots for major players that aren't being used within the current game.
Spoiler :
Code:
-- Tartessos functions - adapted from JFD_NorwayFunctions
-- Author: JFD
-- DateCreated: 6/23/2014 2:36:23 PM
--=======================================================================================================================
-- UTILITY FUNCTIONS	
--=======================================================================================================================
-- JFD_IsCivilisationActive
------------------------------------------------------------------------------------------------------------------------
function JFD_IsCivilisationActive(civilisationID)
	for iSlot = 0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
		local slotStatus = PreGame.GetSlotStatus(iSlot)
		if (slotStatus == SlotStatus["SS_TAKEN"] or slotStatus == SlotStatus["SS_COMPUTER"]) then
			if PreGame.GetCivilization(iSlot) == civilisationID then
				return true
			end
		end
	end

	return false
end
--==========================================================================================================================
-- CORE FUNCTIONS
--==========================================================================================================================
-- Globals
----------------------------------------------------------------------------------------------------------------------------
local civilisationID	= GameInfoTypes["CIVILIZATION_TARTESSOS"]
local isNorwayCivActive = JFD_IsCivilisationActive(civilisationID)
local isNorwayActivePlayer = Players[Game.GetActivePlayer()]:GetCivilizationType() == civilisationID
local mathMin			= math.min

if isNorwayCivActive then
	print("Arganthonios is in this game")
end
----------------------------------------------------------------------------------------------------------------------------
--InitArganthonios
-------------------------------------------------------------------------------------------------------------------------
local terrainCoastID = GameInfoTypes["TERRAIN_COAST"]
local worldHugeID = GameInfoTypes["WORLDSIZE_HUGE"]

function JFD_RevealCoast(playerID)
	local startingPlot = Players[playerID]:GetStartingPlot()
	if startingPlot == nil then return end
	local startingPlotX = startingPlot:GetX()
	local startingPlotY = startingPlot:GetY()
	local teamID = Players[playerID]:GetTeam()
	local distance = 20
	local mapSize = Map.GetWorldSize()
	if mapSize >= worldHugeID then distance = 30 end

	for i = 0, Map.GetNumPlots() - 1, 1 do
		local plot = Map.GetPlotByIndex(i)
		local plotX = plot:GetX()
		local plotY = plot:GetY()
		if (plot:GetTerrainType() == terrainCoastID and Map.PlotDistance(startingPlotX, startingPlotY, plotX, plotY) <= distance) then
			plot:SetRevealed(teamID, true)
		end
	end
end

function JFD_InitArganthonios(player)
	if (Game.GetElapsedGameTurns() <= 1) then
		for playerID = 0, GameDefines.MAX_MAJOR_CIVS - 1 do
			local player = Players[playerID]
			if player:IsEverAlive() and player:IsAlive() then
				if player:GetCivilizationType() == civilisationID then
					JFD_RevealCoast(playerID)
				end
			end
		end
	end
end

if isNorwayCivActive then
	Events.LoadScreenClose.Add(JFD_InitArganthonios)
end
 
Thanks again Lee! I think that's it now!!

Thanks to everyone who has helped in the thread :D.
 
Back
Top Bottom