Novice LUA Programmer - First Project

Sup FP.

I just started my ever long quest into the world of coding. I’ve taken 2 years of computer programming in high school and did a hell of a lot better than most of the others in my class.

So I finally took off my sweaty training bra and started up with a DarkRP server. Yes, I know DarkRP is poorly coded, I’ve experienced this firsthand. And no I didn’t learn anything more from coding into the gamemode either… but I did achieve something.

I found this SWEP:

I wanted a spy job in my DarkRP server, and I wanted him to be able to be invisible when he wanted (at the expense of not being able to attack). Unfortunately, this code was too simple to do that. So I pulled on my thinkers cap and had a small skirmish with tons of stupid mistakes and confusing glitches. Eventually, I had coded a stable(ish) and working version of this invisibility SWEP. It keeps track of the user to see if they take damage, and nulls their cloak if they do. The original looks like this.



-------------Primary Fire Attributes----------------------------------------
function SWEP:PrimaryAttack()

    self.Weapon:SetNextSecondaryFire( CurTime() + 1 )

    local TauntSound = Sound( "vo/npc/male01/overhere01.wav" )

    self.Weapon:EmitSound( TauntSound )

    // The rest is only done on the server
    if (!SERVER) then return end

    self.Weapon:EmitSound( TauntSound )
end

-------------Secondary Fire Attributes-------------------------------------
SWEP.Secondary.Delay        = 0.5
SWEP.Secondary.Recoil        = 0
SWEP.Secondary.Damage        = 0
SWEP.Secondary.NumShots        = 1
SWEP.Secondary.Cone            = 0
SWEP.Secondary.ClipSize        = -1
SWEP.Secondary.DefaultClip    = -1
SWEP.Secondary.Automatic       = false
SWEP.Secondary.Ammo         = "none"

function SWEP:SecondaryAttack() // conceal yourself

if ( !self.conceal ) then
    self.Owner:SetColor( 0, 0, 0, 0, 50 )
    self.Weapon:SetColor( 0, 0, 0, 0, 50 )
    self.Owner:PrintMessage( HUD_PRINTCENTER, "Invisible" )
    self.conceal = true
else
    self.Owner:SetColor( 255, 255, 255, 255 )
    self.Weapon:SetColor( 255, 255, 255 )
    self.Owner:PrintMessage( HUD_PRINTCENTER, "Visible" )
    self.conceal = false
end

end

And my additions can be seen here.



-------------Primary Fire Attributes----------------------------------------
function SWEP:PrimaryAttack()

    self.Weapon:SetNextSecondaryFire( CurTime() + 1 )

    local TauntSound = Sound( "vo/npc/male01/overhere01.wav" )

    self.Weapon:EmitSound( TauntSound )

    // The rest is only done on the server
    if (!SERVER) then return end

    self.Weapon:EmitSound( TauntSound )
end

-------------Secondary Fire Attributes-------------------------------------
SWEP.Secondary.Delay        = 0.5
SWEP.Secondary.Recoil        = 0
SWEP.Secondary.Damage        = 0
SWEP.Secondary.NumShots        = 1
SWEP.Secondary.Cone            = 0
SWEP.Secondary.ClipSize        = -1
SWEP.Secondary.DefaultClip    = -1
SWEP.Secondary.Automatic       = false
SWEP.Secondary.Ammo         = "none"

function SWEP:SecondaryAttack() // conceal yourself

    if ( self.conceal == 0 || !self.conceal ) then
        self.Owner:SetColor( 0, 0, 0, 0, 50 )
        self.Weapon:SetColor( 0, 0, 0, 0, 50 )
        self.Owner:PrintMessage( HUD_PRINTCENTER, "Invisible" )
        self.conceal = 1
        self.lastHealth = self.Owner:Health();
    elseif ( self.conceal == 1 ) then
        self.Owner:SetColor( 255, 255, 255, 255 )
        self.Weapon:SetColor( 255, 255, 255 )
        self.Owner:PrintMessage( HUD_PRINTCENTER, "Visible" )
        self.conceal = 0
    else
        self.Owner:SetColor( 255, 255, 255, 255 )
        self.Weapon:SetColor( 255, 255, 255 )
        self.Owner:PrintMessage( HUD_PRINTCENTER, "Visible" )
        self.conceal = 0
    end
end

function SWEP:Think()

    if (self.conceal == 2) then
        if (self.dullTimer == 0) then
            self.Owner:SetColor( 0, 0, 0, 50, 50 )
            self.Weapon:SetColor( 0, 0, 0, 50, 50 )
        end
        if (self.timerActive) then
            self.dullTimer = self.dullTimer + 1
        end
        if (self.dullTimer >= 50) then
            self.conceal = 1
            self.timerActive = false
            self.Owner:SetColor( 0, 0, 0, 0, 50 )
            self.Weapon:SetColor( 0, 0, 0, 0, 50 )
        end
    elseif (self.conceal == 1) then
        if (self.lastHealth > self.Owner:Health()) then
            self.conceal = 2
            self.dullTimer = 0
            self.timerActive = true
            self.lastHealth = self.Owner:Health()
        end
    else
    end
end

function SWEP:Holster( wep )

    if self.Owner then
        if self.Owner:IsValid() and self.Owner:Alive() then
            self.conceal = 0
            self.dullTimer = 0
            self.timerActive = false
            self.Owner:SetColor( 255, 255, 255, 255 )
            self.Weapon:SetColor( 255, 255, 255 )
            self.Owner:PrintMessage( HUD_PRINTCENTER, "Visible" )
        end
    end
    return true;
end

I admit that it’s just an edit off someone else’s code but this was really my first attempt at coding. I got a satisfying feeling from accomplishing this, even simple, coding project. And of course I plan to code a bunch of my own things, especially gamemodes, and design them in the future.

For now, a simple small accomplishment will do. Also any constructive criticism is appreciated, keep in mind that I don’t code often (yet).

looks cool for a first project.

Did my first project the other day I turned an awp into a buy able weapon for traitors in TTT. I felt high and mighty way to make me feel bad! gj bro!