Help with Spawn Point System.

Hello,
I have a system that allows me to spawn entities on the ground called “ctb_spawnpoint”. At the time of adding them, a value is assigned to them whether they are team init, red or blue. This system works and the entities spawn.

I cannot, however, get a player to spawn on these points. All players are just spawned at Vector(0,0,0). The code find and can list all the different spawns etc, but cannot spawn players at them for some reason. IS it a problem with the spawning system code or maybe the entities themselves?

Spawn System Code:
[lua]
function GM:PlayerSelectSpawn( ply )

local spawns = ents.FindByClass( "ctb_spawnpoint" )
local redspawns = {}
local bluespawns = {}
local initspawns = {}
for k, v in ipairs (spawns) do
if v.SpawnTeam == "Red" then
table.insert(redspawns, v)
end
if v.SpawnTeam == "Blue" then
table.insert(bluespawns, v)
end
if v.SpawnTeam == "Init" then
table.insert(initspawns, v)
end
end


if ply:Team() == 1 then
local random_entry = math.random( #initspawns )
end
if ply:Team() == 2 then
local random_entry = math.random( #redspawns )
end
if ply:Team() == 3 then
local random_entry = math.random( #bluespawns )
end

if ply:Team() == 1 then
return initspawns[random_entry]
elseif ply:Team() == 2 then
return redspawns[random_entry]	
elseif ply:Team() == 3 then
return bluespawns[random_entry]
end

end
[/lua]

Entity Code:

[lua]
ENT.Type = “point”
[/lua]

Sorry, to bother you but this has been getting on my nerves for weeks now.

How did you set the ENT.SpawnTeam?
also wouldn’t it be easier to have 3 different ents?

You need to return the entity for it to work. Here’s an example spawn system that shows how it works ( the point entity is identical by the way ). You move the point entity to the spawn location, then you return it. Additionally, I’d recommend avoiding spawning a point entity at EACH spawn location. It is easier to use one and move it around ( especially since we’re limited to 8k entities, if you create a spawn for each prospective player then there goes 128 entities meanwhile with keeping locations per map and moving 1 entity, you can have more diversity without sacrificing more entities )

https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/playerselectspawn_system.lua.html

As for your code, you’ve made a few mistakes… I added a few comments. Basically we have table.Random to select a random table entry instead of needing to use math.random( #table ) to select one. Also you defined that key inside the scope of an if making it nil when attempting to be used below, which is why we must define the variable within the scope that it will be used in or where it needs to pass through. Additionally, as said in the previous post, where is v.SpawnTeam being set; that is one of the reasons why I say it is easier to define many spawn-points per map and use 1 entity as the designated spawner-point-ent and remove all other “spawn-point-entities”…


local redspawns = { };
local bluespawns = { };
local initspawns = { };

// We shouldn't need to define these.. They should be defined with 
local TEAM_INIT = 1;
local TEAM_RED = 2;
local TEAM_BLUE = 3;

function GM:PlayerSelectSpawn( ply )

	// Set up spawns ONCE unless all 3 tables don't have at least 1 ( and allow auto-refresh to clear memory to regenerate lists )
	if ( !( #redspawns > 0 && #bluespawns > 0 && #initspawns > 0 ) ) then
		// Find all spawn entities
		local spawns = ents.FindByClass( "ctb_spawnpoint" );

		// Process all spawn entities
		for k, v in pairs( spawns ) do
			// Grab the identifier once and string.lower to simplify searching
			local _team = string.lower( v.SpawnTeam );

			if ( _team == "red" ) then
				table.insert( redspawns, v );
			elseif ( _team == "blue" ) then
				table.insert( bluespawns, v );
			elseif ( _team == "init" ) then
				table.insert( initspawns, v );
			else
				local _index = IsValid( v ) && tostring( v ) || -1;
				print( "Error with spawn, no identifier given: " .. tostring( k ) .. ", index: " .. _index .. "!" );
			end
		end
	end

	// Define it within the scope of the main function; if you define a local in the scope of an if, it only exists within
	local random_entry = nil;
	if ( ply:Team( ) == TEAM_INIT ) then
		random_entry = table.Random( initspawns )
	elseif ( ply:Team( ) == TEAM_RED ) then
		random_entry = table.Random( redspawns )
	elseif ( ply:Team( ) == TEAM_BLUE ) then
		random_entry = table.Random( bluespawns )
	else
		error( "Player is not on TEAM INIT/RED/BLUE but is trying to be spawned!" );
	end

	// Return the entry ( no need to grab a random entry key, then grab the entry using a key when we have table.Random( )... )
	return random_entry;
end