DarkRP moneydrop error


[ERROR] gamemodes/darkrp/entities/entities/spawned_money/init.lua:17: Tried to use a NULL physics object!
  1. Wake - [C]:-1
   2. unknown - gamemodes/darkrp/entities/entities/spawned_money/init.lua:17
    3. Spawn - [C]:-1
     4. createMoneyBag - gamemodes/darkrp/gamemode/modules/money/sv_money.lua:46
      5. unknown - gamemodes/darkrp/gamemode/modules/money/sv_money.lua:168


Line 6-18


function ENT:Initialize()
    self:SetModel(GAMEMODE.Config.moneyModel or "models/props/cs_assault/money.mdl")
    self:PhysicsInit(SOLID_VPHYSICS)
    self:SetMoveType(MOVETYPE_VPHYSICS)
    self:SetSolid(SOLID_VPHYSICS)
    self:SetUseType(SIMPLE_USE)
    self:SetCollisionGroup(COLLISION_GROUP_WEAPON)

    local phys = self:GetPhysicsObject()
    self.nodupe = true

    phys:Wake()
end


Line 42-52


function DarkRP.createMoneyBag(pos, amount)
    local moneybag = ents.Create(GAMEMODE.Config.MoneyClass)
    moneybag:SetPos(pos)
    moneybag:Setamount(math.Min(amount, 2147483647))
    moneybag:Spawn()
    moneybag:Activate()
    if GAMEMODE.Config.moneyRemoveTime and  GAMEMODE.Config.moneyRemoveTime ~= 0 then
        timer.Create("RemoveEnt" .. moneybag:EntIndex(), GAMEMODE.Config.moneyRemoveTime, 1, fn.Partial(SafeRemoveEntity, moneybag))
    end
    return moneybag
end


Line 122-174


local function DropMoney(ply, args)
    if args == "" then
        DarkRP.notify(ply, 1, 4, DarkRP.getPhrase("invalid_x", "argument", ""))
        return ""
    end

    if not tonumber(args) then
        DarkRP.notify(ply, 1, 4, DarkRP.getPhrase("invalid_x", "argument", ""))
        return ""
    end
    local amount = math.floor(tonumber(args))

    if amount < 1 then
        DarkRP.notify(ply, 1, 4, DarkRP.getPhrase("invalid_x", "argument", ">0"))
        return ""
    end

    if amount >= 2147483647 then
        DarkRP.notify(ply, 1, 4, DarkRP.getPhrase("invalid_x", "argument", "<2,147,483,647"))
        return ""
    end

    if not ply:canAfford(amount) then
        DarkRP.notify(ply, 1, 4, DarkRP.getPhrase("cant_afford", ""))

        return ""
    end

    ply:addMoney(-amount)
    local RP = RecipientFilter()
    RP:AddAllPlayers()

    umsg.Start("anim_dropitem", RP)
        umsg.Entity(ply)
    umsg.End()
    ply.anim_DroppingItem = true

    timer.Simple(1, function()
        if not IsValid(ply) then return end

        local trace = {}
        trace.start = ply:EyePos()
        trace.endpos = trace.start + ply:GetAimVector() * 85
        trace.filter = ply

        local tr = util.TraceLine(trace)
        local moneybag = DarkRP.createMoneyBag(tr.HitPos, amount)
        hook.Call("playerDroppedMoney", nil, ply, amount, moneybag)
        DarkRP.log(ply:Nick() .. " (" .. ply:SteamID() .. ") has dropped " .. DarkRP.formatMoney(amount))
    end)

    return ""
end

You aren’t checking if the physics object is valid before using it. PhysicsInit can fail if the model has no physics, thus the physobj will be NULL. Wrap all physobj operations with obj:IsValid() before using it, just like an entity.

@code_gs This appears to be from actual DarkRP, not an addon. As for OP’s problem, you probably dont have CS:S installed on the server.

Then an issue on the GitHub should be made.

Its not a problem with the code, its a problem with the physics object not existing because the model for the model doesnt exist. By default, the model for DarkRP money is from CS:S but it appears theres a config option to change it.

It should work even if the model doesn’t: you should always check entity and physics object validity before using it.