IronSights on 357?

Hi there. Uhm…

Yea, i started to script a few days ago, don’t know much and let me make a long story short.

A simple question:

How do you make Ironsights correctly on a 357? i’ve tried everything what a could think about, including what i couldn’t…

Put this into the SWEP.


SWEP.IronSightsPos = Vector (-5.6891, -3.925, 2.5776)
SWEP.IronSightsAng 	= Vector (0.214, -0.1767, 0)

Not sure if this is what you were asking for, or are you talking about toggling on and off the ironsights?

Yes, It is ment to be toggled by Mouse 2. But thanks anyway :3

It would be easier if I saw your code.

Code here:

(The text between the ?'s is the current Ironsight code)

if SERVER then

end

SWEP.Author = “Prune”
SWEP.Contact = “Nope”
SWEP.Purpose = “Shoot!”
SWEP.Instructions = “Figure”
SWEP.Category = “SWeps”

SWEP.Spawnable = false;
SWEP.AdminSpawnable = true;

SWEP.ViewModel = “models/weapons/v_357.mdl”;
SWEP.WorldModel = “models/weapons/w_357.mdl”;

SWEP.Primary.ClipSize = 6;
SWEP.Primary.DefaultClip = 6;
SWEP.Primary.Automatic = false;
SWEP.Primary.Ammo = “357”;
SWEP.IrnSightTIme = 0.25
SWEP.IronSightsPos = Vector(-5.738, -14.263, 4.098)
SWEP.IronSightAng = Vector (-0.575, -0.575, 2.295)

SWEP.Secondary.ClipSize = 0 ;
SWEP.Secondary.DefaultClip = 0 ;
SWEP.Secondary.Automatic = false;
SWEP.Secondary.Ammo = “none”;

SWEP.Sound = Sound ( “weapons/revolvah/deagle-1.wav” )
SWEP.Damage = 50
SWEP.Spread = 0.02
SWEP.NumBul = 1
SWEP.Delay = 0.6
SWEP.Force = 3

function SWEP:Deploy()
self.Weapon:DefaultReload( ACT_VM_DRAW )
self.Weapon:EmitSound(“weapons/revolvah/draw.wav”)
return true

end

function SWEP:Holster()
return true
end

function SWEP:Think()
end
???
function SWEP:IronSight()
end

if self.Owner.KeyPressed(IN_ATTACK2) and !self.Owner:KeyDown(IN_USE) then
    self.Owner:SetFov( 55, 0.15 )
    self.SetIronsights(true, self.Owner)
    self.Owner:SetWalkSpeed(150)
    self.Owner:SetRunSpeed(150)
        
        if CLIENT then return end 
    end
    
    if self.Owner:KeyReleased(IN_ATTACK2) and !self.Owner:KeyDown(IN_USE) then
        
       
        self.Owner:SetFOV( 0, 0.15 )
        self:SetIronsights(false, self.Owner)
        self.Owner:SetWalkSpeed(250)
        self.Owner:SetRunSpeed(500)
        if CLIENT then return end
        
    
end

– I made a single line comment!

/*
I made a multi line comment!
OMG, another line!
And another!
*/

–[[
I made another multi-line comment!
AND ANOTHER LINE!
and another!
etc
]]

function SWEP:SecondaryAttack()
end
function SWEP:GetViewModelPosition( pos, ang )

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

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

if ( bIron!= self.bLatIron ) then

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


end

    
     local fIronTime = self.fIronTime or 0

if ( !bIronTime && fIronTime < CurTime() - self.IronSightTime ) then
    return pos, ang
    
    end
    
          local Mul = 1.0
    
 
    if ( fIronTime > CurTime() - self.IronSightsTime ) then
        
    Mul = math.Clamp( (CurTime() - fIronTime) / self.IronSightTime, 0, 1 )
            
            if (!bIron) then Mul = 1 - Mul 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.IronSightsAnf.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  

/-----------------------------------------------------------
SetIronSights
-----------------------------------------------------------
/
function SWEP:SetIronsights( b )

                    self.Weapons:SetNetworkedBool( "ironsights", b )
                end
                
                SWEP.NextSecondaryAttack = 0
end
                function SWEP:SecondaryAttack()

end

???

self.Weapon:SetNextPrimaryFire( CurTime() + self.Delay )
self.Weapon:SetNextSecondaryFire( CurTime() + self.Delay )
-- this sets the delay for the next primary and secondary fires.

self:TakePrimaryAmmo(1) -- removes 1 ammo from our clip

end – telling Gmod that it’s the end of the function

function SWEP:PrimaryAttack() – when +attack1 happens

-- Make sure we can shoot first
if ( !self:CanPrimaryAttack() ) then return end

local bullet = {} -- creates a table for the properties of the bullet
bullet.Num         = self.NumBul -- number of bullets you are shooting
bullet.Src         = self.Owner:GetShootPos() -- Source, where you are standing
bullet.Dir         = self.Owner:GetAimVector() -- direction of bullet, where you are looking
bullet.Spread     = Vector( self.Spread, self.Spread, 0 ) -- spread of bullet, how accurate it is
bullet.Tracer    = 0    -- this doesn't really affect anything
bullet.Force    = self.Force -- how powerful it is
bullet.Damage    = self.Damage -- how much damage it does to people
bullet.AmmoType = self.Primary.Ammo -- what type of ammo you are using

self.Owner:FireBullets( bullet ) --actually shoots the bullet.

self.Weapon:EmitSound ( self.Sound )
-- this makes the sound, which I specified earlier in the code

self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
-- this makes the shooting animation for the 357

self.Weapon:SetNextPrimaryFire( CurTime() + self.Delay )
self.Weapon:SetNextSecondaryFire( CurTime() + self.Delay )
-- this sets the delay for the next primary and secondary fires.

self:TakePrimaryAmmo(1) -- removes 1 ammo from our clip

end – end our function

function SWEP:Reload()
self.Weapon:DefaultReload( ACT_VM_RELOAD )

end

Please use Code tags next time.


SWEP.Author = "Prune"
SWEP.Contact = "Nope"
SWEP.Purpose = "Shoot!"
SWEP.Instructions = "Figure"
SWEP.Category = "Prune's Sweps"

SWEP.HoldType 		= "pistol"
if (CLIENT) then
	SWEP.PrintName 		= "Magnum"
	SWEP.ViewModelFOV		= 60
	SWEP.Slot 			= 3
	SWEP.SlotPos 		= 1
	SWEP.IconLetter 		= "r"
end


SWEP.Spawnable = false;
SWEP.AdminSpawnable = true;

SWEP.ViewModel = "models/weapons/v_357.mdl";
SWEP.WorldModel = "models/weapons/w_357.mdl";
SWEP.Primary.Delay 		= 0		
SWEP.Primary.ClipSize = 6;
SWEP.Primary.Recoil 		=  10	
SWEP.Primary.DefaultClip = 6;
SWEP.Primary.Cone 		= 0	
SWEP.Primary.Automatic = false;
SWEP.Primary.Ammo = "357";
SWEP.IrnSightTIme = 0.25
SWEP.IronSightsPos = Vector (-5.6891, -3.925, 2.5776)
SWEP.IronSightsAng 	= Vector (0.214, -0.1767, 0)

SWEP.Secondary.ClipSize = 0 ;
SWEP.Secondary.DefaultClip = 0 ;
SWEP.Secondary.Automatic = false;
SWEP.Secondary.Ammo = "none";

SWEP.Sound = Sound ( "weapons/revolvah/deagle-1.wav" )
SWEP.Primary.Damage = 50
SWEP.Spread = 0.02
SWEP.Primary.NumShots = 1
SWEP.Delay = 0.6

function SWEP:Initialize()
	self:SetWeaponHoldType(self.HoldType)
end


function SWEP:Deploy()

	self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
	-- Set the deploy animation when deploying

	self.Reloadaftershoot = CurTime() + 1
	-- Can't shoot while deploying

	self:SetIronsights(false)
	-- Set the ironsight mod to false
	
	self.Weapon:EmitSound("weapons/revolvah/draw.wav")
	
	self.Weapon:SetNextPrimaryFire(CurTime() + 1)
	-- Set the next primary fire to 1 second after deploying

	return true
end

function SWEP:Holster()
	self:SetIronsights(false, self.Owner)
	return true
end

function SWEP:Think()
	self:IronSight()
end

function SWEP:IronSight()

	if !self.Owner:KeyDown(IN_USE) then
	-- If the key E (Use Key) is not pressed, then

		if self.Owner:KeyPressed(IN_ATTACK2) then
		-- When the right click is pressed, then

			self.Owner:SetFOV( 65, 0.15 )

			self:SetIronsights(true, self.Owner)
			-- Set the ironsight true
			self.Owner:SetWalkSpeed(150)
			self.Owner:SetRunSpeed(150)
			
			if CLIENT then return end
 		end
	end

	if self.Owner:KeyReleased(IN_ATTACK2) then
	-- If the right click is released, then
		self.Owner:SetWalkSpeed(250)
		self.Owner:SetRunSpeed(500)
		self.Owner:SetFOV( 0, 0.15 )
		
		self:SetIronsights(false, self.Owner)
		-- Set the ironsight false

		if CLIENT then return end
	end
end


local IRONSIGHT_TIME = 0.15
-- Time to enter in the ironsight mod

function SWEP:GetViewModelPosition(pos, ang)

	if (not 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 (not bIron and 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 not 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



/*-----------------------------------------------------------
SetIronSights
-----------------------------------------------------------*/
function SWEP:SetIronsights( b )
	self:SetNetworkedBool( "ironsights", b )
end

function SWEP:GetIronsights( b )
	self:GetNetworkedBool( "ironsights")
end

function SWEP:RecoilPower()

	if not self.Owner:IsOnGround() then
		if (self:GetIronsights() == true) then
			self:ShootBullet(self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone)
			-- Put normal recoil when you're in ironsight mod

			self.Owner:ViewPunch(Angle(math.Rand(-0.5,-2.5) * (self.Primary.Recoil), math.Rand(-1,1) * (self.Primary.Recoil), 0))
			-- Punch the screen 1x less hard when you're in ironsigh mod
		else
			self:ShootBullet(self.Primary.Damage, self.Primary.Recoil * 2.5, self.Primary.NumShots, self.Primary.Cone)
			-- Recoil * 2.5

			self.Owner:ViewPunch(Angle(math.Rand(-0.5,-2.5) * (self.Primary.Recoil * 2.5), math.Rand(-1,1) * (self.Primary.Recoil * 2.5), 0))
			-- Punch the screen * 2.5
		end

	elseif self.Owner:KeyDown(IN_FORWARD | IN_BACK | IN_MOVELEFT | IN_MOVERIGHT) then
		if (self:GetIronsights() == true) then
			self:ShootBullet(self.Primary.Damage, self.Primary.Recoil / 2, self.Primary.NumShots, self.Primary.Cone)
			-- Put recoil / 2 when you're in ironsight mod

			self.Owner:ViewPunch(Angle(math.Rand(-0.5,-2.5) * (self.Primary.Recoil / 1.5), math.Rand(-1,1) * (self.Primary.Recoil / 1.5), 0))
			-- Punch the screen 1.5x less hard when you're in ironsigh mod
		else
			self:ShootBullet(self.Primary.Damage, self.Primary.Recoil * 1.5, self.Primary.NumShots, self.Primary.Cone)
			-- Recoil * 1.5

			self.Owner:ViewPunch(Angle(math.Rand(-0.5,-2.5) * (self.Primary.Recoil * 1.5), math.Rand(-1,1) * (self.Primary.Recoil * 1.5), 0))
			-- Punch the screen * 1.5
		end

	elseif self.Owner:Crouching() then
		if (self:GetIronsights() == true) then
			self:ShootBullet(self.Primary.Damage, 0, self.Primary.NumShots, self.Primary.Cone)
			-- Put 0 recoil when you're in ironsight mod

			self.Owner:ViewPunch(Angle(math.Rand(-0.5,-2.5) * (self.Primary.Recoil / 3), math.Rand(-1,1) * (self.Primary.Recoil / 3), 0))
			-- Punch the screen 3x less hard when you're in ironsigh mod
		else
			self:ShootBullet(self.Primary.Damage, self.Primary.Recoil / 2, self.Primary.NumShots, self.Primary.Cone)
			-- Recoil / 2

			self.Owner:ViewPunch(Angle(math.Rand(-0.5,-2.5) * (self.Primary.Recoil / 2), math.Rand(-1,1) * (self.Primary.Recoil / 2), 0))
			-- Punch the screen / 2
		end
	else
		if (self:GetIronsights() == true) then
			self:ShootBullet(self.Primary.Damage, self.Primary.Recoil / 6, self.Primary.NumShots, self.Primary.Cone)
			-- Put recoil / 4 when you're in ironsight mod

			self.Owner:ViewPunch(Angle(math.Rand(-0.5,-2.5) * (self.Primary.Recoil / 2), math.Rand(-1,1) * (self.Primary.Recoil / 2), 0))
			-- Punch the screen 2x less hard when you're in ironsigh mod
		else
			self:ShootBullet(self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone)
			-- Put normal recoil when you're not in ironsight mod

			self.Owner:ViewPunch(Angle(math.Rand(-0.5,-2.5) * self.Primary.Recoil, math.Rand(-1,1) *self.Primary.Recoil, 0))
			-- Punch the screen
		end
	end
end

function SWEP:SecondaryAttack()
end

function SWEP:PrimaryAttack() -- when +attack1 happens

-- Make sure we can shoot first
if ( !self:CanPrimaryAttack() ) then return end

self.Reloadaftershoot = CurTime() + self.Primary.Delay
self.Reloadaftershoot = CurTime() + self.Primary.Delay
	-- Set the reload after shoot to be not able to reload when firering
self.Weapon:SetNextPrimaryFire( CurTime() + self.Delay )
self.Weapon:SetNextSecondaryFire( CurTime() + self.Delay )
-- this sets the delay for the next primary and secondary fires.
self:RecoilPower()
self:TakePrimaryAmmo(1) -- removes 1 ammo from our clip
end -- end our function


function SWEP:Reload()

	if ( self.Reloadaftershoot > CurTime() ) then return end 
	-- If you're firering, you can't reload

	self.Weapon:DefaultReload(ACT_VM_RELOAD) 
	-- Animation when you're reloading

	if ( self.Weapon:Clip1() < self.Primary.ClipSize ) and self.Owner:GetAmmoCount(self.Primary.Ammo) > 0 then
	-- When the current clip < full clip and the rest of your ammo > 0, then

		self.Owner:SetFOV( 0, 0.15 )
		-- Zoom = 0

		self:SetIronsights(false)
		-- Set the ironsight to false
	end
end

function SWEP:ShootBullet(dmg, recoil, numbul, cone)

	numbul 		= numbul or 1
	cone 			= cone or 0.01

	local bullet 	= {}
	bullet.Num  	= numbul
	bullet.Src 		= self.Owner:GetShootPos()       					-- Source
	bullet.Dir 		= self.Owner:GetAimVector()      					-- Dir of bullet
	bullet.Spread 	= Vector(cone, cone, 0)     						-- Aim Cone
	bullet.Tracer 	= 1       									-- Show a tracer on every x bullets
	bullet.Force 	= 0.5 * dmg     								-- Amount of force to give to phys objects
	bullet.Damage 	= dmg										-- Amount of damage to give to the bullets
	bullet.Callback 	= HitImpact
-- 	bullet.Callback	= function ( a, b, c ) BulletPenetration( 0, a, b, c ) end 	-- CALL THE FUNCTION BULLETPENETRATION

	self.Owner:FireBullets(bullet)					-- Fire the bullets
	self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK)      	-- View model animation
	self.Owner:MuzzleFlash()        					-- Crappy muzzle light

	self.Owner:SetAnimation(PLAYER_ATTACK1)       			-- 3rd Person Animation

	if ((SinglePlayer() and SERVER) or (not SinglePlayer() and CLIENT)) then
		local eyeang = self.Owner:EyeAngles()
		eyeang.pitch = eyeang.pitch - recoil
		self.Owner:SetEyeAngles(eyeang)
	end
end

This can obviously be expanded upon, but yeah, there you go.

Big thanks to you sir! holy hell! thanks! :slight_smile:

EDIT: Something seems to be happened with the Primary Sound…It doesn’t work… What happened?

[lua]
function SWEP:PrimaryAttack() – when +attack1 happens

– Make sure we can shoot first
if ( !self:CanPrimaryAttack() ) then return end

self.Reloadaftershoot = CurTime() + self.Primary.Delay
self.Reloadaftershoot = CurTime() + self.Primary.Delay
– Set the reload after shoot to be not able to reload when firering
self.Weapon:SetNextPrimaryFire( CurTime() + self.Delay )
self.Weapon:SetNextSecondaryFire( CurTime() + self.Delay )
self.Weapon:EmitSound(self.Primary.Sound)
– this sets the delay for the next primary and secondary fires.
self:RecoilPower()
self:TakePrimaryAmmo(1) – removes 1 ammo from our clip
end – end our function
[/lua]

?

Thanks, But what is that supposed to mean?

Primary Shootsound just… Dissapeared… I used TheDiv’s code and stuff.

How do i fix this?

Sorry, was very tired when I did this.
Change


SWEP.Sound = Sound ( "weapons/revolvah/deagle-1.wav" )

To


SWEP.Primary.Sound = Sound ( "weapons/revolvah/deagle-1.wav" )

and use skullorz code for primary attack.

Aight, thanks :slight_smile: