Getting pos of map entities

I currently have a code where I spawn an entity on the position of an entity already in the map,

function GM:InitPostEntity( )
local copFlag = ents.Create( “ctf_flag” )
local blue = ents.FindByClass(“info_player_blue”)
local bluepos = info_player_blue:GetPos( )
copFlag:SetPos( bluepos )
copFlag:Spawn( )
end

But whenever I run my gamemode I get an error that looks like this:

[ERROR] gamemodes/car/gamemode/init.lua:108: attempt to index global ‘info_player_blue’ (a nil value)

  1. unknown - gamemodes/car/gamemode/init.lua:108

Line 108 of of my init script looks like this:

local bluepos = info_player_blue:GetPos( )

Any help would be appreciated. Thanks!

It means that variable “info_player_blue” is undefined ( aka nil )

Well, edited my code to this:

function GM:InitPostEntity( )
local copFlag = ents.Create( “ctf_flag” )
local flag = ents.FindByClass( “info_player_blue” )
local flagpos = flag:GetPos( )
copFlag:SetPos( flagpos )
copFlag:Spawn( )
end

Now I’m getting an error that looks like this, and btw the entity is definetly in the map, so i dont know how it could be undefined.

[ERROR] gamemodes/car/gamemode/init.lua:108: attempt to call method ‘GetPos’ (a nil value)

  1. unknown - gamemodes/car/gamemode/init.lua:108

Line 108 is:
local flagpos = flag:GetPos( )

ents.FindByClass() returns a table, you’ll either have to loop the table or access the entities using [1], [2] etc.

[lua]
for k,v in ipairs(ents.FindByClass(“info_player_blue”)) do
if !IsValid(v) then continue end
–create your flags here and set their position using ent:SetPos(v:GetPos())
end
[/lua]

Bro you are awesome, thankyou very much!