Is this the best way to solve the problem I'm getting?
1 replies, posted
So, I have a pistol whip for my pistol SWEP's secondary attack, and it works fine. But, in MultiPlayer, I get an error from the client saying that tr.Entity:TakeDamage() is a nil value(it can't be run clientside), because I have my weapon as a shared.lua file. So, here's the whole code:
[code]if SERVER then
AddCSLuaFile("shared.lua")
end
SWEP.PrintName = "Pistol"
SWEP.Author = ""
SWEP.Contact = ""
SWEP.Purpose = ""
SWEP.Instructions = ""
SWEP.Base = "dm_base_hl2"
SWEP.UseHands = true
SWEP.Category = "The Rebel SWEPs"
SWEP.Spawnable = true
SWEP.AdminSpawnable = true
SWEP.ViewModelFlip = false
SWEP.ViewModel = "models/weapons/c_pistol.mdl"
SWEP.WorldModel = "models/weapons/w_pistol.mdl"
SWEP.ViewModelFOV = 54
SWEP.AutoSwitchTo = true
SWEP.AutoSwitchFrom = true
SWEP.Slot = 0
SWEP.SlotPos = 0
SWEP.HoldType = "pistol"
SWEP.FiresUnderwater = false
SWEP.Weight = 25
SWEP.ShowViewModel = true
SWEP.ShowWorldModel = true
SWEP.Primary.ClipSize = 18
SWEP.Primary.Ammo = "Pistol"
SWEP.Primary.DefaultClip = 72
SWEP.Primary.Automatic = false
SWEP.Primary.Force = 1
SWEP.Primary.Spread = 1
SWEP.Primary.Recoil = 4
SWEP.Primary.Delay = 0.1
SWEP.Primary.NumberofShots = 1
SWEP.Primary.Cone = 0.1
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.Ammo = "none"
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Damage1 = 20
SWEP.Damage2 = 30
SWEP.RecoilUp = 3
SWEP.RecoilDown = -3
SWEP.RecoilSideLeft = 2
SWEP.RecoilSideRight = -2
SWEP.PrimSpread = 0.50
SWEP.WalkSpread = 1
SWEP.Primary.WalkSpread = 1.25
SWEP.Primary.WepSpread = 0.75
SWEP.Primary.IdleSpread = 0.75
SWEP.SwepSound = "Weapon_Pistol.NPC_Single"
SWEP.FireRate = 0.10
if SERVER then
SWEP.Name = "pistol"
end
if CLIENT then
killicon.AddFont( "dm_pistol", "HL2MPTypeDeath", "-", Color( 255, 80, 0, 255 ) )
end
--pistol whip functionality
function SWEP:SecondaryAttack()
self.Weapon:EmitSound( "WeaponFrag.Throw" )
self.Owner:ViewPunch( Angle( 15, 15, 15 ) )
self.Owner:DoCustomAnimEvent( PLAYERANIMEVENT_ATTACK_GRENADE , 123 )
self.Weapon:SendWeaponAnim( ACT_VM_LOWERED_TO_IDLE )
self:SetNextIdle( CurTime() + self:SequenceDuration() )
local tracedata = {}
tracedata.start = self.Owner:GetShootPos()
tracedata.endpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * 75
tracedata.filter = self.Owner
tracedata.mins = Vector( -8 , -8 , -8 )
tracedata.maxs = Vector( 8 , 8 , 8 )
if ( self.Owner:IsPlayer() ) then
self.Owner:LagCompensation( true )
end
local tr = util.TraceHull( tracedata )
if ( self.Owner:IsPlayer() ) then
self.Owner:LagCompensation( false )
end
if tr.Hit then
print( tr.Entity ) --your code here
self.Weapon:EmitSound( "Flesh.ImpactHard" )
--[[if ( tr.Entity:IsPlayer() ) then
if SERVER then --this was my method of choice to solve this
tr.Entity:TakeDamage( 40 )]]--please note that these comment brackets are here to emphasize its importance, in the real code the comment brackets aren't in here
end
end
end
self:SetNextSecondaryFire( CurTime() + 0.40 )
self:SetNextPrimaryFire( CurTime() + 0.60 )
end
hook.Add( "DoAnimationEvent" , "AnimEventTest" , function( ply , event , data )
if event == PLAYERANIMEVENT_ATTACK_GRENADE then
if data == 123 then
ply:AnimRestartGesture( GESTURE_SLOT_ATTACK_AND_RELOAD, ACT_HL2MP_GESTURE_RANGE_ATTACK_MELEE, true )
end
return ACT_INVALID
end
end )
[/code]
As you can see, my method to solve this was to use an [code]if SERVER then[/code] if statement. Is this the best way to do so? Can anyone tell me what a better way would be if one exists?
Yes, it's the only way to put it server side if it's in a shared file. There are no problems using "if SERVER then".
P.S: I think you should give better titles to your threads, because this gives only little insight on what's your issue.
Sorry, you need to Log In to post a reply to this thread.