Can anyone help? I want to make bolt-shooting SWEP.

Hello, i want to make SWER that shoot crossbow bolts. Name of entity is crossbow_bolt. Can anyone give me function to the SWEP?

[editline]13th March 2015[/editline]

I don’t have much time. Please help.

What kind of help are you looking for?

Help learning Lua?

Help understanding the Gmod API?

Or would you like someone to write the whole thing for you, for free?

I just need SWEP (Scripted weapon) that shoots crossbow bolts. I want to make automatic crossbow :\

So, you want someone to write the code for you? For free?

yes, if anyone is experienced at LUA, it should not be a huge problem or waste of time. I am lua noob :\

Nobody will code it for free, unless they are feeling bored or just generous. You shouldn’t come here expecting such.

I’m feeling pretty generous.



function SWEP:PrimaryAttack()
    if !self:CanPrimaryAttack() then return end
    self:SetNextPrimaryFire(CurTime() + 0.75)
    if SERVER then
        local Bolt = ents.Create("crossbow_bolt")
        if !IsValid(Bolt) then return end
        Bolt:SetPos(self.Owner:GetShootPos() + self.Owner:EyeAngles():Forward()*50 - self.Owner:EyeAngles():Up()*3 + self.Owner:EyeAngles():Right()*3)
        Bolt:SetAngles(self.Owner:EyeAngles()) --[[* self.Owner:GetEyeTrace().Fraction*2000]]
        Bolt:Spawn()
        Bolt:SetOwner(self.Owner)
        Bolt:SetVelocity(Bolt:GetAngles():Forward()*3447.2)
        Bolt.SetPhysicsAttacker(self.Owner)
        --Bolt:SetDamage(500) This is the part that's not working yet
    end
end


This is the primary fire function and it shoots bolts. Good luck getting damage to work properly because I don’t know how to do that (pssst, if anyone does, please tell me).

I saw on an earlier post that it was something like `bolt.m_iDamage = 500’ or something, but I’m not too sure how correct that was.

[editline]14th March 2015[/editline]

The crossbow’s normal velocity is 3500, by the way. You’ll want to multiply the player’s forward vector by that, as such;


Bolt:SetVelocity(self.Owner:GetAimVector() * 3500)

[editline]14th March 2015[/editline]

Honestly, you can simplify that code a lot;



function SWEP:PrimaryAttack()
	if not self:CanPrimaryAttack() then return end
	self.Weapon:SetNextPrimaryFire( CurTime() + 0.75 )	
	self:EmitSound(shootSound)

	if SERVER then
		local bolt = ents.Create("crossbow_bolt")
		if not IsValid(bolt) then return end

		bolt:SetPos( self.Owner:EyePos() + (self.Owner:GetAimVector() * 48) )
		bolt:SetAngles( self.Owner:EyeAngles() )
		bolt:SetOwner(self.Owner)
		bolt.m_iDamage = 100
		bolt:Spawn()

		bolt:SetVelocity(self.Owner:GetAimVector() * 3500)
	end
end


Thanks. Unfortunately though, the bolt.m_iDamage does not work. I also tried it before. It seems to deliver 0 damage but still trigger the TakeDamage hook. It also prints this in the console whenever it hits something:

“AddMultiDamage: g_MultiDamage.GetDamageForce() == vec3_origin”

I have no idea what to do with this :frowning:

Thanks! :smiley:

[editline]14th March 2015[/editline]

But, i have question, because i always had some problems with bolt-shooting SWEP. Does bolt have collision with players, NPCs and props?

Oh, i have an idea! You probably didn’t add force variable.

Let me know what you come up with then.

I will try to fix it, i know some other variables from other weapon entities. Ahh, force is how powerful is this, not how many damage deals.

The solution is to index a unique variable on the bolt entity, something like



bolt.BFGsCrossbowDamage = 100


then set up an EntityTakeDamage hook that looks for this variable specifically, then sets damage based on it:



hook.Add("EntityTakeDamage", "BFGBoltFix", function(target, dmginfo)
	local inflictor = dmginfo:GetInflictor()
	if inflictor:GetClass() == "crossbow_bolt" and inflictor.BFGsCrossbowDamage then
		dmginfo:SetDamage(inflictor.BFGsCrossbowDamage)
	end
end)


EDIT: Fixed for code inconsistency

Ooookay, i will try the solution. But, i am not very good at LUA, where i have to put it?

[editline]15th March 2015[/editline]

I’ve got a script error: [ERROR] addons/auto-bow swep/lua/weapons/weapon_autobow/shared.lua:64: bad argument #1 to ‘SetDamage’ (number expected, got nil)

  1. SetDamage - [C]:-1
  2. fn - addons/auto-bow swep/lua/weapons/weapon_autobow/shared.lua:64
    3. unknown - addons/ulib/lua/ulib/shared/hook.lua:183

[editline]15th March 2015[/editline]

Hey, i fixed the error, but ho to apply damage force?

use: dmginfo:SetDamage( inflictor.NoPBowDamage || 0 ); so if NoPBowDamage isn’t set, it uses 0 ( make sure that variable does use a number if you use it though )…

Oh sorry, this was from one of my own SWEPs and I forgot to replace that variable with the correct one I used in the example :stuck_out_tongue:

[editline]16th March 2015[/editline]

original post is fix’d

BFG9000, how to apply damage force to the entity?

I know this, but i am getting beautiful error when i add this “self.Owner:GetAimVector * 50”