Anticrash Lua Error

Hello again Facepunch!
I am recieving this error:


[ERROR] lua/autorun/server/anticrash.lua:43: attempt to call method 'Reset' (a nil value)
1. AMB_SetSubPhysMotionEnabled - lua/autorun/server/anticrash.lua:43
2. AMB_KillVelocity - lua/autorun/server/anticrash.lua:70
3. fn - lua/autorun/server/anticrash.lua:22
4. unknown - addons/ulib/lua/ulib/shared/hook.lua:183

from this script:
[lua]local freezespeed = 750
local removespeed = 2000
hook.Add(“Think”,“AMB_CrashCatcher”,function()
for k, ent in pairs(ents.FindByClass(“prop_ragdoll”)) do
if IsValid(ent) then
if ent.player_ragdoll then
local velo = ent:GetVelocity( ):Length()
local ply = false
if IsValid(ent:GetOwner()) then
ply = ent:GetOwner()
end
if velo >= removespeed then
ent:Remove()
ServerLog("[!CRASHCATCHER!] Caught ragdoll entity moving too fast ("…velo…"), removing offending ragdoll entity from world.
“)
local messageToShow = “[CRASH PREVENTION] A ragdoll was removed to prevent server crashing. It was "
if CORPSE.GetFound(ent, false) then
PrintMessage(HUD_PRINTTALK, messageToShow … “an unID’d body.”)
else
PrintMessage(HUD_PRINTTALK, messageToShow … ent:GetNWString(“nick”) … “'s body.”)
end
elseif velo >= freezespeed then
AMB_KillVelocity(ent)
ServerLog(”[!CRASHCATCHER!] Caught ragdoll entity moving too fast (”…velo…"), disabling motion.
")
end
end
end
end
end)

function AMB_SetSubPhysMotionEnabled(ent, enable)
if not IsValid(ent) then return end

    ent:SetVelocity(vector_origin)
   
    if !(enable) then
            ent:SetColor(Color(255,0,255,255))
    else
            ent:SetColor(Color(255,255,255,255))
    end
   
    if !(enable) then
            if IsValid(ent:GetOwner()) then
                    ent:GetOwner():GetWeapon("weapon_zm_carry"):Reset(false)
            end
            --ent:SetPos(ent:GetPos()+Vector(0,0,50))
    end

    for i=0, ent:GetPhysicsObjectCount()-1 do
            local subphys = ent:GetPhysicsObjectNum(i)
            if IsValid(subphys) then
                    subphys:EnableMotion(enable)
                    if !(enable) then
                            subphys:SetVelocity(vector_origin)
                            subphys:SetMass(subphys:GetMass()*20)
                    end
                    if enable then
                            subphys:SetMass(subphys:GetMass()/20)
                            subphys:Wake()
                    end
            end
    end
   
   
   
    ent:SetVelocity(vector_origin)

end

function AMB_KillVelocity(ent)
AMB_SetSubPhysMotionEnabled(ent, false)
timer.Simple(3, function() AMB_SetSubPhysMotionEnabled(ent, true) end)
end[/lua]
Any idea how I could fix this? Thank you in advance for any given support :slight_smile:



 if IsValid(ent:GetOwner()) then
                        ent:GetOwner():GetWeapon("weapon_zm_carry"):Reset(false)
                end

Add a check here for IsValid(ent:GetOwner():GetWeapon(“weapon_zm_carry”))

Will that work if the player doesn’t have the default crowbar?

weapon_zm_carry is magneto stick, not a crowbar.

Still the same problem. Not all players magneto sticks are called that since purchasable replacements is an option.

Since I have replacements could I do


 if IsValid(ent:GetOwner()) then
                        ent:GetOwner():GetWeapon("weapon_zm_carry", "other_weapon", "other_weapon2", "etc"):Reset(false)

Would that work?

No.
Something like this:



if ( IsValid( ent:GetOwner():GetWeapon("weapon_zm_carry") ) then
     ent:GetOwner():GetWeapon("weapon_zm_carry"):Reset(false)
end

And copy paste this for all weps.

Or use beautiful tables :

[lua]
for _, wep in pairs( { “weapon_zm_carry”, “something_else”, “something_else2” } ) do

if ( IsValid( ent:GetOwner():GetWeapon( wep ) ) ) then 

    ent:GetOwner():GetWeapon( wep ):Reset( false );

end 

end
[/lua]

Thank you :slight_smile:

This seems to be appearing:


[ERROR] lua/autorun/server/anticrash.lua:76: attempt to index global 'ent' (a nil value)
1. unknown - lua/autorun/server/anticrash.lua:76

Solved this, put it in wrong :stuck_out_tongue:

Sorry for the bump but I am now getting this error:


[ERROR] lua/autorun/server/anticrash.lua:44: attempt to call method 'GetWeapon' (a nil value)
  1. AMB_SetSubPhysMotionEnabled - lua/autorun/server/anticrash.lua:44
   2. AMB_KillVelocity - lua/autorun/server/anticrash.lua:74
    3. fn - lua/autorun/server/anticrash.lua:22
     4. unknown - addons/ulib/lua/ulib/shared/hook.lua:183


Thanks :slight_smile: