LUA:Gamemode from Scratch Part Two

From GMod Wiki

Jump to: navigation, search
Lua: Part two
Page white text.png Description:Continues the tutorial.
link=User:Philip Dyplin Original Author:Philip Dyplin
Calendar.png Created:2nd July, 2009

Contents

Now we get more stuff in

What will you learn?

So let's get started

team.SetUp( 3, "Warrior", Color( 100, 100, 100, 255 ) )
 
team.SetUp( 4, "Admin", Color( 0, 255, 255, 255 ) )

So, now we have two more teams. We have a warrior team and an admin team.

function GM:PlayerLoadout( ply )
 
if ply:Team() == 1 then
 
    ply:Give( "weapon_physcannon" ) --Give the player the Gravity Gun
    ply:SetModel( "models/player/kleiner.mdl" ) --Set the players model
    ply:Give( "item_battery", 5 ) --Give them 5 battries
 
elseif ply:Team() == 2 then
 
    ply:Give( "weapon_physgun" )
    ply:SetModel( "models/player/Eli.mdl" )
 
elseif ply:Team() == 3 then
 
    ply:Give( "weapon_pistol" )
    ply:GiveAmmo( 180, "pistol" ) --Give the player 180 pistol rounds
    ply:SetModel( "models/player/barney.mdl" )
 
elseif ply:Team() == 4 then
 
    ply:Give( "weapon_physgun" )
    ply:Give( "weapon_357" )
    ply:GiveAmmo( 32, "357" )
    ply:SetModel( "models/player/gman_high.mdl" )
 
end
 
end
 


Add two more buttons for the teams and two more commands for the teams.

--Edit the frame
 frame:SetSize( 300, 300 ) //Set the size
 
--Now add the two other buttons
 
 team_3 = vgui.Create( "DButton", frame ) 
 team_3:SetPos( 135, 30 ) //Place it next to our previous one 
 team_3:SetSize( 100, 50 ) 
 team_3:SetText( "Team 3" ) 
 team_3.DoClick = function() //Make the player join team 3 
 
     RunConsoleCommand( "team_3" ) 
 
 end 
 
 team_4 = vgui.Create( "DButton", frame ) 
 team_4:SetPos( 135, 85 ) //Place it next to our previous one 
 team_4:SetSize( 100, 50 ) 
 team_4:SetText( "Team 4" ) 
 team_4.DoClick = function() //Make the player join team 4 
 
     RunConsoleCommand( "team_4" ) 
 
 end 
 
 
function team_3( ply ) 
 
    ply:SetTeam( 3 ) //Make the player join team 3 
    ply:Spawn()
end 
 
function team_4( ply ) 
 
    ply:SetTeam( 4 ) //Make the player join team 4 
    ply:Spawn()
 
end 
--And after the two previous console commands add
 
concommand.Add( "team_3", team_3 ) //Add the command to set the players team to team 3
concommand.Add( "team_4", team_4 ) //Add the command to set the players team to team 4
 
 

Now we have made four teams, added a loadout for each of them, and edited the menu so you can choose team.

Set a custom/existing spawn point

In init.lua add

function GM:PlayerSelectSpawn( ply ) --Make a team select a spawn and randomize where they spawn
 
local spawns = ents.FindByClass( "info_player_start" ) --Make so that we can use spawns instead of info_player_start (they will do the same things)
     local random = math.random(spawns) --Random spawn of the info_player_start  
 
if ply:Team() == 1 --If player is in team 1
then
 
spawns[random] --Make them always spawn from a random point of this class
 
end --End if condition
 
end --End the function
 

This was requested and may or may not work as I don't have Garry's Mod available at the moment.

Links

Part one.

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox