SWEP - IronSights

I’ve never played around with SWEP’s before, and I wanted to start by making IronSights but I have no idea where to start…

I assumed it would be changing the FOV so I did this…

The NWBool is for a Check somewhere else in my Code.

[lua]
function SWEP:SecondaryAttack()

if ( self.Owner:GetNWBool( "IronSights", false ) ) then
    self.Owner:SetNWBool( "IronSights", true )
    self.Owner:SetFOV( 30, 0, 20 )
    self.Owner:SetWalkSpeed( 100 )
    self.Owner:SetRunSpeed( 100 )
        if ( SERVER ) then
            self.Owner:DrawViewModel( false )
        end
elseif ( self.Owner:GetNWBool( "IronSights", true ) ) then
        self.Owner:SetNWBool( "IronSights", false )
        self.Owner:SetFOV( 0, 0, 20 )
        self.Owner:SetWalkSpeed( 250 )
        self.Owner:SetRunSpeed( 500 )
            if ( SERVER ) then
                self.Owner:DrawViewModel( true )
            end
end

end
[/lua]

I’d be grateful if someone had an example of code.

All this does is set the FOV, Playerspeed, And turn the ironsights to “True”, Without any legitimate value. The most common method of creating IronSights is utilizing the ‘GetViewModelPosition’ Code.
Referr to this:
http://wiki.garrysmod.com/?title=SWEP.GetViewModelPosition
As for some example code, I can probably help you out there too.
[lua]
–Respectfully taken from Garry’s weapon_cs_base
local IRONSIGHT_TIME = 0.25

/---------------------------------------------------------
Name: GetViewModelPosition
Desc: Allows you to re-position the view model
---------------------------------------------------------
/
function SWEP:GetViewModelPosition( pos, ang )

if ( !self.IronSightsPos ) then return pos, ang end

local bIron = self.Weapon:GetNetworkedBool( "Ironsights" )

if ( bIron != self.bLastIron ) then

	self.bLastIron = bIron 
	self.fIronTime = CurTime()
	
	if ( bIron ) then 
		self.SwayScale 	= 0.3
		self.BobScale 	= 0.1
	else 
		self.SwayScale 	= 1.0
		self.BobScale 	= 1.0
	end

end

local fIronTime = self.fIronTime or 0

if ( !bIron && fIronTime < CurTime() - IRONSIGHT_TIME ) then 
	return pos, ang 
end

local Mul = 1.0

if ( fIronTime > CurTime() - IRONSIGHT_TIME ) then

	Mul = math.Clamp( (CurTime() - fIronTime) / IRONSIGHT_TIME, 0, 1 )
	
	if (!bIron) then Mul = 1 - Mul end

end

local Offset	= self.IronSightsPos

if ( self.IronSightsAng ) then

	ang = ang * 1
	ang:RotateAroundAxis( ang:Right(), 		self.IronSightsAng.x * Mul )
	ang:RotateAroundAxis( ang:Up(), 		self.IronSightsAng.y * Mul )
	ang:RotateAroundAxis( ang:Forward(), 	self.IronSightsAng.z * Mul )


end

local Right 	= ang:Right()
local Up 		= ang:Up()
local Forward 	= ang:Forward()



pos = pos + Offset.x * Right * Mul
pos = pos + Offset.y * Forward * Mul
pos = pos + Offset.z * Up * Mul

return pos, ang

end
[/lua]
or instead of putting this into your code, you can do a HUGE Sidestep and just base the weapon on weapon_cs_base, Then use these two lines somewhere in the shared to define an ironsight.
[lua]
SWEP.IronSightsPos = Vector( 0, 0, 0 )
SWEP.IronSightsPos = Vector( 0, 0, 0 )
[/lua]
If you need help finding the correct vector for your ironsights, Here is a nifty little SWEP to help you do so.
http://www.garrysmod.org/img/?t=dll&id=29787
let me know how this helps!

Ah thank you very much, that’s really helpful!