Error in PlayerDoTurn

Starrynite120

Prince
Joined
Jul 15, 2015
Messages
472
I'm getting an error in this function that I do not understand why I am getting.

Code:
function GetPoints()
	local Points = 1 --ERROR 1
	local PLAYERPERK = GameInfo.PlayerPerks["PLAYERPERK_KNOWLEDGE_1"].ID;
	local PLAYERPERK2 = GameInfo.PlayerPerks["PLAYERPERK_INDUSTRY_1"].ID;
	local Player = Players[playerID];

	if (Player:HasPerk(PLAYERPERK)) then --ERROR 2
		Points = Points + 1
	end

	if (Player:HasPerk(PLAYERPERK2)) then
		Points = Points + 1
	end

	if (Points > 2) then
		Game.SetWinner(Players[Game.GetActivePlayer()]:GetTeam(), GameInfo.Victories["VICTORY_TIME"].ID);
	else
		Points = 1
	end
end
GameEvents.PlayerDoTurn.Add(GetPoints);

The error is:
Code:
 Runtime Error: C:\Users\Starrynite120\Documents\My Games\Sid Meier's Civilization Beyond Earth\MODS\Mods\Into the Nebula (v 1)\Victories/StarryNitePointVictory.lua:116: attempt to index a nil value
stack traceback:
	C:\Users\Starrynite120\Documents\My Games\Sid Meier's Civilization Beyond Earth\MODS\Mods\Into the Nebula (v 1)\Victories/StarryNitePointVictory.lua:116: in function 'GetPoints'
stack traceback:
	C:\Users\Starrynite120\Documents\My Games\Sid Meier's Civilization Beyond Earth\MODS\Mods\Into the Nebula (v 1)\Victories/StarryNitePointVictory.lua:111: in function 'GetPoints'

What am I getting a nil value error?

To give some background to what I'm trying to do, I'm trying to detect if each player has certain playerperks and then declare them winner if they have the right ones. Its my attempt at a new victory condition. (I know the others are done through quests, which I may try and do instead once I get this one working.)
 
It's hard to say without seeing the whole of your file.

Which line is line 116? that's the line where your problem's happening. One of the variables you're trying to reference on that line, whichever line of code that is, is nil.

If it helps, you can turn on the display of line numbers under the "text editor" tab in the Modbuddy Tools-->Options menu.
 
The rest of the file is commented out, and line 116 I marked --ERROR 2, as well as 111 which I marked --ERROR 1

But I don't know why its nil because I assigned the values
 
Code:
function GetPoints()
...
	local Player = Players[[COLOR="Red"]playerID[/COLOR]];

	if (Player:HasPerk(PLAYERPERK)) then --ERROR 2
...
PlayerID isn't defined. So you don't have a player object. So you're calling HasPerk on a null value.
 
Ah that did it. And good news, the Game.SetWinner works from CiV. Pretty easy way to make a new victory.
 
Top Bottom