damage drop off

trying to make damage drop off but i get the error:
AddMultiDamage: g_MultiDamage.GetDamageForce() == vec3_origin

i used this for the damage drop off inside the swep


local function BulletCallback(att, tr, dmg)  
	if ValidEntity(att) then
		local dist = (tr.HitPos - tr.StartPos):Length()
		
		if dist > (70 * (512 / 9.75)) then
			dmg:ScaleDamage(0.3)
		elseif dist > (10 * (512 / 9.75)) then
			dmg:ScaleDamage(200 - ((133 / 60) * (dist / (512 / 9.75))))
		elseif dist > 0 then
			dmg:ScaleDamage(1)
		end
	end
end

please help

bump

That means the damage force is not being set. Could you post full code?


AddCSLuaFile()

if CLIENT then
    SWEP.PrintName = "AK47"
    SWEP.Author = "DarkRP Developers"
    SWEP.Slot = 3
    SWEP.SlotPos = 0
    SWEP.IconLetter = "b"

    killicon.AddFont("weapon_ak472", "CSKillIcons", SWEP.IconLetter, Color(255, 80, 0, 255))
end

SWEP.Base = "weapon_cs_base2"

SWEP.Spawnable = true
SWEP.AdminOnly = false
SWEP.Category = "DarkRP (Weapon)"

SWEP.UseHands = true
SWEP.ViewModel = "models/weapons/cstrike/c_rif_ak47.mdl"
SWEP.WorldModel = "models/weapons/w_rif_ak47.mdl"

SWEP.Weight = 5
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false

SWEP.HoldType = "ar2"

SWEP.Primary.Sound = Sound("Weapon_AK47.Single")
SWEP.Primary.Recoil = 1.0
SWEP.Primary.Damage = 200
SWEP.Primary.NumShots = 1
SWEP.Primary.Cone = 0.0001
SWEP.Primary.ClipSize = 30
SWEP.Primary.Delay = 0.12
SWEP.Primary.DefaultClip = 90
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = "smg1"

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"

SWEP.IronSightsPos = Vector(0, -2, 2)
SWEP.IronSightsAng = Vector(0, 0, 0)

SWEP.MultiMode = true

SWEP.DarkRPBased = false

function SWEP:DrawHUD()
	draw.DrawText("+", "TargetID", ScrW() * 0.5, ScrH() * 0.5, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER)
end

local function BulletCallback(att, tr, dmg)  
	if ValidEntity(att) then  
		local dist = (tr.HitPos - tr.StartPos):Length()
		
		if dist > (70 * (512 / 9.75)) then
			dmg:ScaleDamage(0.3)
		elseif dist > (10 * (512 / 9.75)) then
			dmg:ScaleDamage(200 - ((133 / 60) * (dist / (512 / 9.75))))
		elseif dist > 0 then
			dmg:ScaleDamage(1)
		end
	end
end

darkrp ak47

BulletCallback is just a local function – you aren’t doing anything with it, which means it won’t work.

ok what can i do to fix this

I don’t know what you want to happen – can you describe in words what your goal is?

i want to make damage scale at distances, so the distance between the two players is found and then damage reduced with y = mx+c formulas
i did it a while ago but lost the code and forgot

I think he wanted that function being called by EntityTakeDamage

ok now i get this error, im doing the trace wrong… how do i fix this?

[ERROR] gamemodes/darkrp/entities/weapons/weapon_ak472/shared.lua:60: attempt to perform arithmetic on field ‘HitPos’ (a nil value)

  1. fn - gamemodes/darkrp/entities/weapons/weapon_ak472/shared.lua:60
  2. unknown - addons/ulib/lua/ulib/shared/hook.lua:109
    3. FireBullets - [C]:-1
    4. CSShootBullet - gamemodes/darkrp/entities/weapons/weapon_cs_base2/shared.lua:234
    5. unknown - gamemodes/darkrp/entities/weapons/weapon_cs_base2/shared.lua:194

Check the arguments of

GM:EntityTakeDamage. The second one is a CTakeDamageInfo object, not a trace. To get the position where damage occurred you’d use

CTakeDamageInfo:GetDamagePosition.

what does this mean?

AddMultiDamage: g_MultiDamage.GetDamageForce() == vec3_origin

Don’t use EntityTakeDamage or it will apply to non-weapon damage. Instead, add the callback in an EntityFireBullets hook.

im stuck, can someone give me an example

-snip-


hook.Add("EntityFireBullets", "DamageDropoff", function(_, tbl)
   local fOldCallback = tbl.Callback

   if (fOldCallback == nil) then
      tbl.Callback = BulletCallback
   else
      tbl.Callback = function(...)
         BulletCallback(...)
         return fOldCallback(...)
      end
   end
end)