Hi guys, just a noob passing through.
So um, I've been trying to make a SWEP these past few that would shoot Crossbow bolts. But when the Crossbow bolts hit an NPC, they get 0 damage. Anybody know why and how I could change the damage?
Here's whats inside the SWEP's shared.lua file:
SWEP.Author = ""
SWEP.Instructions = ""
SWEP.Spawnable = true
SWEP.AdminOnly = true
SWEP.UseHands = true
SWEP.ViewModel = "models/weapons/v_irifle.mdl"
SWEP.WorldModel = "models/weapons/w_irifle.mdl"
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = "none"
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false
SWEP.PrintName = "Bolt Launcher"
SWEP.Slot = 1
SWEP.SlotPos = 2
SWEP.DrawAmmo = false
local ShootSound = Sound( "Weapon_Crossbow.Single" )
/*---------------------------------------------------------
Initialize
---------------------------------------------------------*/
function SWEP:Initialize()
self:SetHoldType( "ar2" )
end
/*---------------------------------------------------------
Reload does nothing
---------------------------------------------------------*/
function SWEP:Reload()
end
/*---------------------------------------------------------
PrimaryAttack
---------------------------------------------------------*/
function SWEP:PrimaryAttack()
self:SetNextPrimaryFire( CurTime() + 0.1 )
self:EmitSound( ShootSound )
self:ShootEffects( self )
-- The rest is only done on the server
if (!SERVER) then return end
local Forward = self.Owner:EyeAngles():Forward()
local ent = ents.Create( "crossbow_bolt" )
if ( IsValid( ent ) ) then
ent:SetPos( self.Owner:GetShootPos() + Forward * 32 )
ent:SetAngles( self.Owner:EyeAngles() )
ent:Spawn()
ent:SetVelocity( Forward * 3500 )
ent:SetOwner( self.Owner )
end
end
/*---------------------------------------------------------
SecondaryAttack
---------------------------------------------------------*/
function SWEP:SecondaryAttack()
end
/*---------------------------------------------------------
Name: ShouldDropOnDie
Desc: Should this weapon be dropped when its owner dies?
---------------------------------------------------------*/
function SWEP:ShouldDropOnDie()
return false
end
There's no way to fix that as far as I know.
Eventually he could use AddCallback("PhysicsCollide") and add damage to the object hit. Or even create a custom entity that acts as the bolt.
[lua]hook.Add("EntityTakeDamage", "crossbowbolt_dmg", function(ent, dmg)
local inf = dmg:GetInflictor()
if IsValid(inf) and inf:GetClass() == "crossbow_bolt" and dmg:GetDamage() == 0 then
dmg:SetDamage(100)
end
end)[/lua]
Crossbow bolts created from Lua still inflict damage but for some reason the amount is always 0. You can hook into EntityTakeDamage like I did above to manually set the damage amount yourself.
[QUOTE=Dear_Matt;45734956]Eventually he could use AddCallback("PhysicsCollide")[/QUOTE]
Crossbow bolts don't have physics.
You could try using ent:SetSaveValue( "m_iDamage", 100 ) but I don't think that's gonna work like it does on the rockets because the bolt doesn't have that value in the data descriptor.
Alternatively you could go the hacky way and force the damage to be whatever you want from the EntityTakeDamage hook , which is what people did for quite a while.
[lua]
if SERVER then
hook.Add("EntityTakeDamage" , "CrossbowBolt - damage override" , function( ent , info )
local inflictor = info:GetInflictor()
if IsValid( inflictor ) && inflictor:GetClass() == "crossbow_bolt" && info:GetDamage() == 0 then
info:SetDamage( 100 ) -- your custom damage value
end
end)
end
[/lua]
for people looking at that here's that post in code brackets:
[CODE]SWEP.Author = ""
SWEP.Instructions = ""
SWEP.Spawnable = true
SWEP.AdminOnly = true
SWEP.UseHands = true
SWEP.ViewModel = "models/weapons/v_irifle.mdl"
SWEP.WorldModel = "models/weapons/w_irifle.mdl"
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = "none"
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false
SWEP.PrintName = "Bolt Launcher"
SWEP.Slot = 1
SWEP.SlotPos = 2
SWEP.DrawAmmo = false
local ShootSound = Sound( "Weapon_Crossbow.Single" )
/*---------------------------------------------------------
Initialize
---------------------------------------------------------*/
function SWEP:Initialize()
self:SetHoldType( "ar2" )
end
/*---------------------------------------------------------
Reload does nothing
---------------------------------------------------------*/
function SWEP:Reload()
end
/*---------------------------------------------------------
PrimaryAttack
---------------------------------------------------------*/
function SWEP:PrimaryAttack()
self:SetNextPrimaryFire( CurTime() + 0.1 )
self:EmitSound( ShootSound )
self:ShootEffects( self )
-- The rest is only done on the server
if (!SERVER) then return end
local Forward = self.Owner:EyeAngles():Forward()
local ent = ents.Create( "crossbow_bolt" )
if ( IsValid( ent ) ) then
ent:SetPos( self.Owner:GetShootPos() + Forward * 32 )
ent:SetAngles( self.Owner:EyeAngles() )
ent:Spawn()
ent:SetVelocity( Forward * 3500 )
ent:SetOwner( self.Owner )
end
end
/*---------------------------------------------------------
SecondaryAttack
---------------------------------------------------------*/
function SWEP:SecondaryAttack()
end
/*---------------------------------------------------------
Name: ShouldDropOnDie
Desc: Should this weapon be dropped when its owner dies?
---------------------------------------------------------*/
function SWEP:ShouldDropOnDie()
return false
end[/CODE]
[QUOTE=Jvs;45735065]You could try using ent:SetSaveValue( "m_iDamage", 100 ) but I don't think that's gonna work like it does on the rockets because the bolt doesn't have that value in the data descriptor.
Alternatively you could go the hacky way and force the damage to be whatever you want from the EntityTakeDamage hook , which is what people did for quite a while.
[lua]
if SERVER then
hook.Add("EntityTakeDamage" , "CrossbowBolt - damage override" , function( ent , info )
local inflictor = info:GetInflictor()
if IsValid( inflictor ) && inflictor:GetClass() == "crossbow_bolt" && info:GetDamage() == 0 then
info:SetDamage( 100 ) -- your custom damage value
end
end)
end
[/lua][/QUOTE]
I have already tried the SaveValues, it doesn't work unfortunately. EntityTakeDamage is the only way to fix it and I don't like it.
Cool, it actually worked. Thanks for the help Robotboy, Matt, Kilburn & Jvs.
Sorry, you need to Log In to post a reply to this thread.