Setting Custom Spawn Points In Garry's Mod With Lua Scripts

In this tutorial, I will be walking through the code to set custom spawn locations in Garry's Mod. This code will reset the default spawn locations in a map so that new one can be coded in.

Getting the Spawn Location Table

First we have to get the spawn location table which holds all the information about the entities used for spawning in the player. We do this with the following code.

local spawns = ents.FindByClass( "info_player_start" )

This code will return a table of all entities with the class "info_player_start". Now we have to remove the default entities from this new table.

Removing Default Spawn Locations

Next we have to remove these entities so that they will no longer be used to spawn in players. We can do this with the following code.

for k,v in pairs(spawns) do
    v:Remove()
end

It is important to note that this will specifically tell the entity to get removed. While working on this script, I had issues with this problem as I was only removing the entities from the table and not removing them from the game. Now we can add our own spawn points in.

Adding Custom Spawn Points

Now lets add our custom spawn point in. To create a new spawn point we will be using the ents library which helps creating and finding entities. Here is the code to do that.

local spawn_point = ents.Create( "info_player_start" )
spawn_point:SetPos( Vector(10.0, 10.0, 100.0) )
spawn_point:SetAngles( Angle (3.0, 3.0, 0.0 ) )

First we use ents.Create to create a new "info_player_start" and set that to a local variable so that we can change its position and rotation. Then we set the position and rotation that we want for it. Note that these values are set to default to be zeros so the spawn location will be at the world origin and have no rotation applied initially.

Running The Script In Game

Running the script in game is very simple. To do it, save the code into a file. Then move that file to GarrysMod\garrysmod\lua. Then to run the script in game, use the lua_openscript command in the in game terminal. The command should take one parameter which will be the file to in which your code is in. For example, if the file is called, resetspawnpoint.lua, then you should run lua_openscript resetspawnpoint.lua to execute the script.

Full Code

-- Get table of spawn point entities
local spawns = ents.FindByClass( "info_player_start" )

-- Remove default spawn points
for k,v in pairs(spawns) do
    v:Remove()
end

-- Add custom spawn points
local spawn_point = ents.Create( "info_player_start" )
spawn_point:SetPos( Vector(10.0, 10.0, 100.0) )
spawn_point:SetAngles( Angle (3.0, 3.0, 0.0 ) )
© 2022, Jake D'Esposito. Creative Commons License