Change this:
Code:
-- Change this
local bagbg = CreateFrame("Frame", bagbg, contname)
-- ...to this.
local bagbg = CreateFrame("Frame", nil, _G[contname])
In your current code, you're setting the global name of bagbg to something undefined. I set it to nil, because you'll probably not be accessing it globally. You're also attempting to parent it to a name (string), which isn't possible - I've changed it to actually parent to the globally-accessible frame of that name.