SWEP self is nil?

I have this code in a SWEP.

For whatever reason, the code won’t get the table I have set.

The error I get:




function SWEP:DrawWorldModel()

    SWEP.WElements = {
	["JSrighthandcuff"] = { type = "Model", model = "models/props_vehicles/carparts_tire01a.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(0, 0, 0), angle = Angle(10, 110, -13.44), size = SWEP:GetCuffSize("ValveBiped.Bip01_R_Hand"), color = Color(160, 160, 160, 255), surpresslightning = false, material = "phoenix_storms/cube", skin = 0, bodygroup = {} },
	["JSlefthandcuff"] = { type = "Model", model = "models/props_vehicles/carparts_tire01a.mdl", bone = "ValveBiped.Bip01_L_Hand", rel = "", pos = Vector(0, .2, 0), angle = Angle(-10, 110, 7), size = SWEP:GetCuffSize("ValveBiped.Bip01_L_Hand"), color = Color(160, 160, 160, 255), surpresslightning = false, material = "phoenix_storms/cube", skin = 0, bodygroup = {} }
    }

end





function SWEP:GetCuffSize(boneroo)

	if self == nil then return end
	if boneroo == nil then return end

	print(boneroo)

	local ply = self:GetOwner()

	local function lookupbone(lubone) -- Gets the bone id
		return ply:LookupBone(lubone)
	end

	local JSHBGroups = ply:GetHitBoxGroupCount() -- The amount of groups of hitboxes in the ent
    local JSHBXS = {} -- My little table to store the hitboxs I need
    JSHBXS.LeftHandHB = {}
    JSHBXS.RightHandHB = {}

    for k=0, JSHBGroups - 1 do -- Loops through all the hitbox groups with i = the hitbox group num
        local HBCount = ply:GetHitBoxCount(k) -- The amount of hitboxes in the hitbox group
        
        for v=0, HBCount do -- Loop through all the hitboxes in the group 'i'
            -- k now = the hitbox group and v = the hitbox in that group
            local mybone = ply:GetHitBoxBone(v,k) -- Gets a bone based on k,v

            if mybone == lookupbone(boneroo)  then -- RBS.LHand is the bone id for Left hand
                JSHBXS.LeftHandHB["Group"] = k
                JSHBXS.LeftHandHB["HB"] = v
            elseif mybone == lookupbone(boneroo) then -- Same thing with right hand
                JSHBXS.RightHandHB["Group"] = k
                JSHBXS.RightHandHB["HB"] = v
            end
        end
    end

    -- Lets call these two after this for loop
	local JLH = JSHBXS.LeftHandHB -- So I don't have to type it all out
	local JRH = JSHBXS.RightHandHB

    -- Now we should have the hitboxes as needed.
	local function jsvecmath(boney)


		local function jsquickr(hand)
			return ply:GetHitBoxBounds(hand["Group"], hand["HB"]) -- returns the vectors of hitbox
		end

	    if boney == "ValveBiped.Bip01_L_Hand" then -- If we're dealing with the left hand
	    	local jvmin, jvmax = jsquickr(JLH) -- HB first, then group
	    	local x1,y1,z1 = jvmin
	    	local x2,y2,z2 = jvmax
	    	local x1 = math.abs(x1 - x2) -- get the distance between the two amirite?
	    	local z1 = math.abs(z1 - z2) -- get the distance between the two amirite?
	    	return Vector(x1,.1,z1)

	    elseif boney == "ValveBiped.Bip01_R_Hand" then -- If we're dealing with the right hand
	    	local jvmin, jvmax = jsquickr(JRH) -- HB first, then group
	    	local x1,y1,z1 = jvmin
	    	local x2,y2,z2 = jvmax
	    	local x1 = math.abs(x1 - x2) -- get the distance between the two amirite?
	    	local z1 = math.abs(z1 - z2) -- get the distance between the two amirite?
	    	return Vector(x1,.1,z1)
	   
	    else
	    	return Vector(.17, .1, .17)
	    end
	end

	local jscaledvec = jsvecmath(boneroo) -- Gets the scaled vector

	if jscaledvec == nil then
		return (Vector(.17, .1, .17))
	else
		return jscaledvec
	end
  
-- Edit x and z

end



Change SWEP to self inside of a meta function. Also, you should really just restructure your code to determine the size at render time instead of updating the table every time.

Nah that’s not it.

I’m trying right now but I think what the problem is, is when I set the right and left hand “HB” and “Group”.

I’m not actually checking if it is the right or left bone. Something screws up there

Yep, that was it. On to the next error.

Oh my god, what are you even trying to code, please, structure your code correctly, i’m impressed that the code even runs in gmod, or even try

Stop creating methods inside another methods please

It might not be your biggest problem but it will still stop your code from working just the same.a