Kill Counter

From GMod Wiki

Revision as of 13:15, 11 August 2010 by Cube334 (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Intro

Hello, I am Sphinxa279 (Kurtis Wright) long time Facepunch lurker and Garrysmod player, writing my first tutorial here, facepunch helped me learn this and seeing as it's not here I thought I'd help you guys over here.

This is a handy Lua snippet just to implement into your gamemode, what this handy snippet here does is add a kill counter just above your HUD's health counter, then everytime a player kills an NPC it will add 1 to this kill counter, the one I am showing you will change to a map and print a message when a user achieves the required amount of kills.

Here goes.

Init.lua

So, already at the top of your init.lua file you should have:

AddCSLuaFile( "cl_init.lua" )

if not, go ahead and add that now, as another part of the counter will be in the cl_init.lua

Done that? Good.

Now, in your init.lua place this little snippet of text here:

function KillCounter( victim, killer, weapon )  --Sets up a new function called KillCounter
        if killer:GetNWInt("killcounter") == 50 then --If the killcounter variable equals 50 then do something 
            PrintMessage(HUD_PRINTTALK, "Player" .. killer:GetName() .. "Has Won")  --When the killcounter equals 50 it will print this "Player <playername> has won
            timer.Simple(3, function()   --Sets up a timer for three seconds
                game.ConsoleCommand("changelevel " ..table.Random(maps).. "\n") --When the timer finishes it excecutes this console command   
            end)  
        end  
            killer:SetNWInt("killcounter", killer:GetNWInt("killcounter") + 1)  --Adds 1 everytime an NPC is killed.
    end

Okay so now you have that, you can see that I use:

game.ConsoleCommand("changelevel " ..table.Random(maps).. "\n")

This is calling from a table which we will now create, just above/below the previous code snippet place this:

local maps = {"gm_construct", "gm_flatgrass", "cs_office", "cs_italy"}

Now, that's just an example, but what the main script will do is, when someone reaches 50 kills (on NPCs) then it will select a map from this table at random and then change to that map, simple huh?

Now we need to finish this all off with a hook, I tend to place these at the bottom of my init.lua, you can place it where you like though:

hook.Add("OnNPCKilled","KillCounter", KillCounter)

All this does is hook our function to everytime an NPC is killed.

cl_init.lua

Now we move onto placing a small chunk of code within the cl_init.lua file, all this code does is draw the killcounter just above the HUD's health counter:

function killcounter()  
        draw.WordBox( 8, ScrW() - 920, ScrH() - 98, "KillCount: "..LocalPlayer():GetNWInt("killcounter"),"ScoreboardText",Color(200,0,0,0),Color(255,255,255,255))  
    end  
    hook.Add("HUDPaint","KillCounter",killcounter)  



The end

There you have it, when you go ingame now and run your gamemode it should have your killcounter as shown here:

tutorialimage.png

--Glad I could help 15:15, 20 June 2010 (UTC)

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox