Hi! Can someone help me with this code I have below. I'm no lua programmer and making this semi-working pistol took a long time for me. It works fine in single player, but glitches a lot in multiplayer. Can someone fix them, point the problems or guide me to the right direction with what I'm doing wrong?
cl_init.lua
[CODE]
include('shared.lua')
SWEP.PrintName = "Military Grade Pistol"
SWEP.Instructions = "Aim & fire!"
SWEP.Slot = 5
SWEP.SlotPos = 1
SWEP.DrawAmmo = false
SWEP.DrawCrosshair = false
SWEP.UseHands = true
SWEP.SetHoldType = "pistol"
SWEP.ViewModelFlip = false
SWEP.Slot = 2
SWEP.SlotPos = 0
SWEP.DrawAmmo = true
SWEP.DrawCrosshair = true
[/CODE]
init.lua
[CODE]
AddCSLuaFile ("cl_init.lua")
AddCSLuaFile ("shared.lua")
include ("shared.lua")
SWEP.Weight = 5
SWEP.AutoSwitchTo = true
SWEP.AutoSwitchFrom = false
[/CODE]
shared.lua
[CODE]
AddCSLuaFile()
SWEP.SetHoldType = "pistol"
if CLIENT then
SWEP.PrintName = "weapon_e_poorpistol"
SWEP.Slot = 2
SWEP.ViewModelFlip = false
SWEP.IconLetter = "B"
end
SWEP.Base = "weapon_base"
SWEP.SetHoldType = "pistol"
SWEP.Weight = 5
SWEP.AutoSwitchTo = true
SWEP.AutoSwitchFrom = false
SWEP.DrawAmmo = true
SWEP.DrawCrosshair = true
SWEP.Spawnable = true
SWEP.AdminSpawnable = true
SWEP.ShouldDropOnDie = true
SWEP.Primary.ClipSize = 8
SWEP.Primary.DefaultClip = 64
SWEP.Primary.Ammo = "Pistol"
SWEP.Primary.AutRecoil = 3
SWEP.Primary.Damage = 14
SWEP.Primary.NumShots = 1
SWEP.Primary.Spread = 0.05
SWEP.Primary.Cone = 3
SWEP.Primary.Delay = 0.25
SWEP.Primary.TakeAmmo = 1
SWEP.Primary.Automatic = false
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Ammo = "none"
SWEP.Secondary.Automatic = false
SWEP.ViewModel = "models/weapons/v_pist_fiveseven.mdl"
SWEP.WorldModel = "models/weapons/w_pist_fiveseven.mdl"
SWEP.UseHands = true
local waitingforreload = false
local sndFire = Sound("totod/fiveseven/fiveseven-1.wav")
function SWEP:Deploy()
self.Owner:GetViewModel():SetPlaybackRate(0.9)
self.Owner:GetViewModel():SetColor(Color(150, 150, 150, 255))
waitingforreload = false
end
function SWEP:DrawWorldModel()
self:DrawModel()
end
function SWEP:Equip()
self.Owner:GetViewModel():SetPlaybackRate(0.9)
waitingforreload = false
end
function SWEP:PrimaryAttack()
if(waitingforreload) then return end
if ( self.Weapon:Clip1() <= 0 ) then
self:EmitSound( "Weapon_Pistol.Empty" )
self:SetNextPrimaryFire( CurTime() + 0.2 )
self:Reload()
return
end
self:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
self:EmitSound( sndFire )
local bullet = {}
bullet.Num = self.Primary.NumShots
bullet.Src = self.Owner:GetShootPos()
bullet.Dir = self.Owner:GetAimVector()
bullet.Spread = Vector( self.Primary.Spread, self.Primary.Spread, 0 )
bullet.Tracer = 1
bullet.TracerName = "Tracer"
bullet.Force = 1
bullet.Damage = self.Primary.Damage
bullet.AmmoType = self.Primary.Ammo
self.Owner:FireBullets( bullet )
self:ShootEffects()
self.Owner:ViewPunch( Angle( -1, 0, 0 ) )
if ( self:Clip1() <= 0 ) then
if ( self:Ammo1() <= 0 ) then return end
self.Owner:RemoveAmmo( self.Primary.TakeAmmo, self:GetPrimaryAmmoType() )
return end
self:SetClip1( self:Clip1() - self.Primary.TakeAmmo )
end
function SWEP:ShootEffects()
self:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
self.Owner:MuzzleFlash()
self.Owner:SetAnimation( PLAYER_ATTACK1 )
end
function SWEP:CanSecondaryAttack()
return false
end
function SWEP:Reload()
if(!waitingforreload) then
waitingforreload = true;
self:DefaultReload(ACT_VM_RELOAD)
self.Owner:GetViewModel():SetPlaybackRate(0.9)
timer.Simple(3.5, function() waitingforreload = false end)
end
end
[/CODE]
[quote]but glitches a lot in multiplayer[/quote]
What do you mean by that? Are there any Lua errors? What is happening that shouldn't be happening?
[QUOTE=JasonMan34;52269621]What do you mean by that? Are there any Lua errors? What is happening that shouldn't be happening?[/QUOTE]
Hi,
Some examples:
- The weapon not firing
- The weapon firing and reloading (both of the animations are played at the same time) ==> the bullet flies but no damage or impact is made, no ammo is taken
--> Occasionally it shoots a real bullet that has impact
Thanks!
*edit*
no lua errors
A note before I start, this may not fix it but it may help in terms of orginisation and other miscellaneous.
Are the files all contained within a parent folder? What is the intended function of the gun, simply to shoot a player and damage them?
Folder ensures that when you call the weapon, you are giving the weapon all files that the weapon needs are included, not neccessarily needed but I find it useful.
Intended function, just makes sure there isn't anything missing.
Try using: [url]http://wiki.garrysmod.com/page/Global/IsFirstTimePredicted[/url]
[QUOTE=Gl1tch3t;52270889]A note before I start, this may not fix it but it may help in terms of orginisation and other miscellaneous.
Are the files all contained within a parent folder? What is the intended function of the gun, simply to shoot a player and damage them?
Folder ensures that when you call the weapon, you are giving the weapon all files that the weapon needs are included, not neccessarily needed but I find it useful.
Intended function, just makes sure there isn't anything missing.
Try using: [url]http://wiki.garrysmod.com/page/Global/IsFirstTimePredicted[/url][/QUOTE]
Thanks! I was thinking it was the local variable that was messing things up so I fixed some stuff based on your link. Can you check if the code seems OK now?
shared.lua
[CODE]
AddCSLuaFile()
if CLIENT then
SWEP.PrintName = "weapon_e_poorpistol"
SWEP.Slot = 2
SWEP.ViewModelFlip = false
SWEP.IconLetter = "B"
end
SWEP.Base = "weapon_base"
SWEP.Weight = 5
SWEP.AutoSwitchTo = true
SWEP.AutoSwitchFrom = false
SWEP.DrawAmmo = true
SWEP.DrawCrosshair = true
SWEP.Spawnable = true
SWEP.AdminSpawnable = true
SWEP.ShouldDropOnDie = true
SWEP.Primary.ClipSize = 8
SWEP.Primary.DefaultClip = 64
SWEP.Primary.Ammo = "Pistol"
SWEP.Primary.AutRecoil = 3
SWEP.Primary.Damage = 14
SWEP.Primary.NumShots = 1
SWEP.Primary.Spread = 0.05
SWEP.Primary.Cone = 3
SWEP.Primary.Delay = 0.25
SWEP.Primary.TakeAmmo = 1
SWEP.Primary.Automatic = false
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Ammo = "none"
SWEP.Secondary.Automatic = false
SWEP.ViewModel = "models/weapons/v_pist_fiveseven.mdl"
SWEP.WorldModel = "models/weapons/w_pist_fiveseven.mdl"
SWEP.UseHands = true
local sndFire = Sound("totod/fiveseven/fiveseven-1.wav")
function SWEP:Initialize()
self:SetHoldType( "pistol" )
self:SetWeaponHoldType( "pistol" )
end
function SWEP:Deploy()
self.Owner:GetViewModel():SetPlaybackRate(0.9)
end
function SWEP:DrawWorldModel()
self:DrawModel()
end
function SWEP:Equip()
self.Owner:GetViewModel():SetPlaybackRate(0.9)
end
function SWEP:PrimaryAttack()
if(!self:CanPrimaryAttack()) then
return
end
if ( self.Weapon:Clip1() <= 0 ) then
self:EmitSound( "Weapon_Pistol.Empty" )
self:SetNextPrimaryFire( CurTime() + 0.2 )
self:Reload()
return
end
self:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
ply:LagCompensation( true )
self:EmitSound( sndFire )
local bullet = {}
bullet.Num = self.Primary.NumShots
bullet.Src = self.Owner:GetShootPos()
bullet.Dir = self.Owner:GetAimVector()
bullet.Spread = Vector( self.Primary.Spread, self.Primary.Spread, 0 )
bullet.Tracer = 1
bullet.TracerName = "Tracer"
bullet.Force = 1
bullet.Damage = self.Primary.Damage
bullet.AmmoType = self.Primary.Ammo
self.Owner:FireBullets( bullet )
self:ShootEffects()
self.Owner:ViewPunch( Angle( -1, 0, 0 ) )
if ( self:Clip1() <= 0 ) then
if ( self:Ammo1() <= 0 ) then return end
self.Owner:RemoveAmmo( self.Primary.TakeAmmo, self:GetPrimaryAmmoType() )
return end
self:SetClip1( self:Clip1() - self.Primary.TakeAmmo )
ply:LagCompensation( false )
end
function SWEP:ShootEffects()
self:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
self.Owner:MuzzleFlash()
self.Owner:SetAnimation( PLAYER_ATTACK1 )
end
function SWEP:CanSecondaryAttack()
return false
end
function SWEP:Reload()
self:DefaultReload(ACT_VM_RELOAD)
self:SetNextPrimaryFire( CurTime() + 3.5 )
end
[/CODE]
Sorry about the late reply, I'm experiencing technical issues with GMOD at the moment.
I can't see any glaring problems but this: [QUOTE][CODE] if ( self:Clip1() <= 0 ) then
if ( self:Ammo1() <= 0 ) then return end
self.Owner:RemoveAmmo( self.Primary.TakeAmmo, self:GetPrimaryAmmoType() )
return end
self:SetClip1( self:Clip1() - self.Primary.TakeAmmo )
ply:LagCompensation( false )[/CODE][/QUOTE] could probably go in this function [url]https://wiki.garrysmod.com/page/WEAPON/CanPrimaryAttack[/url]
so [CODE]function SWEP:CanPrimaryAttack()
if ( self:Clip1() <= 0 ) then
if ( self:Ammo1() <= 0 ) then return end
self.Owner:RemoveAmmo( self.Primary.TakeAmmo, self:GetPrimaryAmmoType() )
return end
self:SetClip1( self:Clip1() - self.Primary.TakeAmmo )
ply:LagCompensation( false )
end[/CODE]
And the reason I prefer this over putting it in my PrimaryAttack, depending on what you are doing, and how the CanPrimaryAttack is setup in the base weapon, it may call this twice, better to overwrite the base if you are going to use it at all. Instead of removing 1 bullet, it removes 2. - I personally had issues with this because I wasn't paying attention to what was happening. You may not need to, you may not even need this snippet of code at all, but if anything is going to be odd, this is what I can see it being. Other than that, without proper testing, I can't really see an issue here.
Best of luck
[QUOTE=Gl1tch3t;52290506]And the reason I prefer this over putting it in my PrimaryAttack, depending on what you are doing, and how the CanPrimaryAttack is setup in the base weapon, it may call this twice[/QUOTE]
Which is why it says [URL="https://wiki.garrysmod.com/page/WEAPON/PrimaryAttack"]on the wiki on the first line[/URL] to check [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/IsFirstTimePredicted]IsFirstTimePredicted[/url]. Don't use CanPrimaryAttack.
[QUOTE=MPan1;52290522]Which is why it says [URL="https://wiki.garrysmod.com/page/WEAPON/PrimaryAttack"]on the wiki on the first line[/URL] to check [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/IsFirstTimePredicted]IsFirstTimePredicted[/url]. Don't use CanPrimaryAttack.[/QUOTE]
Upon further looking at the code and Wiki (something I wasn't able to do at the time), you are indeed correct. Atomic, use IsFirstTimePredicted, ignore my earlier statement.
Sorry, you need to Log In to post a reply to this thread.