A bit confused

I am trying to save the old bone angles from a player and have them be restored when the player switches weapons.

Says the table “urtable” is nil, not sure why. It should be defined.


local function restrain(ply)
 
    local function getcurBones()
 
        local boneyc = ply:GetBoneCount()
        local curboneTable = {}
 
        while boneyc >= 0 do
            curboneTable[boneyc] = ply:GetManipulateBoneAngles(boneyc)
            boneyc = boneyc - 1
        end
 
        return curboneTable
    end
 
    local oldboneAng = getcurBones()
 
    local UnrestrainData = {}
    UnrestrainData.MOVETYPE = ply:GetMoveType()
    UnrestrainData.BonePos =
 
    ply:SetMoveType(0)
    ply:SetNWBool("CurRestrained", true)
 
    local RBS = {}
    RBS.Head = ply:LookupBone("ValveBiped.Bip01_Head1")
    RBS.Neck = ply:LookupBone("ValveBiped.Bip01_Neck1")
    RBS.Spine1 = ply:LookupBone("ValveBiped.Bip01_Spine1")
    RBS.Spine2 = ply:LookupBone("ValveBiped.Bip01_Spine2")
    RBS.Spine3 = ply:LookupBone("ValveBiped.Bip01_Spine4")
    RBS.Pelvis = ply:LookupBone("ValveBiped.Bip01_Pelvis")
    RBS.LShoulder = ply:LookupBone("ValveBiped.Bip01_L_Clavicle")
    RBS.RShoulder = ply:LookupBone("ValveBiped.Bip01_R_Clavicle")
    RBS.LUpArm = ply:LookupBone("ValveBiped.Bip01_L_UpperArm")
    RBS.RUpArm = ply:LookupBone("ValveBiped.Bip01_R_UpperArm")
    RBS.LFArm = ply:LookupBone("ValveBiped.Bip01_L_Forearm")
    RBS.RFArm = ply:LookupBone("ValveBiped.Bip01_R_Forearm")
    RBS.LHand = ply:LookupBone("ValveBiped.Bip01_L_Hand")
    RBS.RHand = ply:LookupBone("ValveBiped.Bip01_R_Hand")
    RBS.LThigh = ply:LookupBone("ValveBiped.Bip01_L_Thigh")
    RBS.RThigh = ply:LookupBone("ValveBiped.Bip01_R_Thigh")
    RBS.LCalf = ply:LookupBone("ValveBiped.Bip01_L_Calf")
    RBS.RCalf = ply:LookupBone("ValveBiped.Bip01_R_Calf")
    RBS.LFoot = ply:LookupBone("ValveBiped.Bip01_L_Foot")
    RBS.RFoot = ply:LookupBone("ValveBiped.Bip01_R_Foot")
 
    ply:ManipulateBoneAngles(RBS.Head, Angle(0,0,0))
    ply:ManipulateBoneAngles(RBS.Neck, Angle(0,0,0))
    ply:ManipulateBoneAngles(RBS.Spine1, Angle(0,20,0))
    --ply:ManipulateBoneAngles(RBS.Spine2, Angle())
    --ply:ManipulateBoneAngles(RBS.Spine3, Angle())
    --ply:ManipulateBoneAngles(RBS.Pelvis, Angle())
    --ply:ManipulateBoneAngles(RBS.LShoulder, Angle())
    --ply:ManipulateBoneAngles(RBS.RShoulder, Angle())
    --ply:ManipulateBoneAngles(RBS.LUpArm, Angle())
    --ply:ManipulateBoneAngles(RBS.RUpArm, Angle())
    --ply:ManipulateBoneAngles(RBS.LFArm, Angle())
    --ply:ManipulateBoneAngles(RBS.RFArm, Angle())
    --ply:ManipulateBoneAngles(RBS.LHand, Angle())
    --ply:ManipulateBoneAngles(RBS.RHand, Angle())
    --ply:ManipulateBoneAngles(RBS.LThigh, Angle())
    --ply:ManipulateBoneAngles(RBS.RThigh, Angle())
    --ply:ManipulateBoneAngles(RBS.LCalf, Angle())
    --ply:ManipulateBoneAngles(RBS.RCalf, Angle())
    --ply:ManipulateBoneAngles(RBS.LFoot, Angle())
    --ply:ManipulateBoneAngles(RBS.RFoot, Angle())
 
    return oldboneAng
 
end
 
local function unrestrain(ply, urtable)
 
    local rbones = ply:GetBoneCount()
 
    while rbones >= 0 do
 
        ply:ChatPrint(tostring(urtable[rbones]))
 
        ply:ManipulateBoneAngles(rbones, urtable[rbones])
 
        rbones = rbones - 1
    end
 
end
 
function SWEP:Initialize()
 
    self:SetHoldType("passive")
 
end
 
function SWEP:Equip(ply)
 
    local rtabled = restrain(ply)
    self.urboneangles = rtabled
 
end
 
hook.Add("PlayerSwitchWeapon", "Unrestrain", function(ply, wep, newwep)
 
    if ply:IsValid() then
        if ply:Alive() and ply:GetNWBool("CurRestrained") then
            if wep:IsValid() then
                if wep:GetClass() == "police_restrained" then
 
                    unrestrain(ply, wep.urboneangles)
       
                end
            end
        end
    end
 
end)

SWEP:Equip is only called serverside. You’ll either have to network it, find a different weapon hook, or only access the table serverside. Also,


    UnrestrainData.BonePos =
 
    ply:SetMoveType(0)

You have an empty equivalence statement here, and you should use an enum instead of an absolute number.

Yea, I kinda forgot I made a table for the whole data, I was trying to test just the bones for now.
Thanks, I think I’ll network it. Getting a different hook will make it a hassle getting it to work properly.

[editline]4th December 2016[/editline]

Also, why should I use an ENUM instead of a number?

It’s possible that the next time gmod is updated, all the move types will get a different value. If you use MOVETYPE_NONE instead of 0, it will always use the correct value.

ply:SetMoveType(MOVETYPE_NONE)

It also makes the code more readable. Let’s say you read this code next week, and you see 0, you ask “what was 0 again?” and you have to open the wiki to remember. If it says MOVETYPE_NONE you automatically know what it is.