'GetWeapon' Lua error

The error I am getting is this:


[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

and the script I am using is this:
[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
            for _, wep in pairs( { "weapon_zm_carry", "weapon_glowstick_rng", "weapon_glowstick_blu", "weapon_glowstick", "weapon_glowstick_red", "weapon_glowstick_lblu", "weapon_glowstick_ylw", "weapon_fish", "gidzco_gms", "weapon_glowstick_wht", "weapon_icesword" } ) do

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

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

                   end 
            end
    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]
The issue spams the console so much and I think is removing some functionality from the script such as freezing the bodies.
As usual, any help is greatly appreciated :slight_smile:

Are you sure that the Owner of “ent” is always a player?

[editline]8th June 2014[/editline]

Because it has to be a player in order for GetWeapon to exsit.

[editline]8th June 2014[/editline]

Try putting



if IsValid(ent:GetOwner()) and ent:GetOwner():IsPlayer() then
--stuff
end


In there

Where would I place that because I’m not sure what you meant by stuff :stuck_out_tongue: