• Removing DButtons
    4 replies, posted
so I'm having a problem where when I press a button that starts a function that adds buttons to the derma then when I try to press another button that does MyButtonNameHere:Remove() I get an error and the button(s) aren't removed, anyone have an idea to where I wont get an error and the button will be removed?
What's the error? [editline]18th June 2016[/editline] Also, some code would help
[QUOTE=MPan1;50542811]What's the error? [editline]18th June 2016[/editline] Also, some code would help[/QUOTE] [CODE]local frame = vgui.Create("DFrame") frame:SetSize(500,500) frame:Center() frame:SetVisible(true) frame:ShowCloseButton(false) frame:SetDraggable(false) frame:SetTitle("") frame:MakePopup() frame.Paint = function(s,w,h) draw.RoundedBox(0,0,0,w,h,Color(0,0,0,225)) end DerW, DerH = frame:GetSize() local But = vgui.Create("DButton",frame) But:SetSize(150,50) But:SetPos(DerW/2 - 75, DerH/2 - 25) But:SetText("Creates buttons") But.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, Color( 255, 255, 255, 255 ) ) end But.DoClick = function() But:Remove() AddButtons() end local C = vgui.Create("DButton",frame) C:SetSize(150,50) C:SetPos(DerW/2 - 75,450) C:SetText("Close buttons") C.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, Color( 255, 255, 255, 255 ) ) end C.DoClick = function() A:Remove() end function AddButtons() local A = vgui.Create("DButton",frame) A:SetSize(150,50) A:SetPos(0,DerH/2 - 25) A:SetText("Does nothing") A.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, Color( 255, 255, 255, 255 ) ) end local B = vgui.Create("DButton",frame) B:SetSize(150,50) B:SetPos(200,DerH/2 - 25) B:SetText("Does nothing") B.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, Color( 255, 255, 255, 255 ) ) end end[/CODE] Gets this error [CODE][ERROR] lua/rekt.lua:34: attempt to index global 'A' (a nil value) 1. DoClick - lua/rekt.lua:34 2. unknown - lua/vgui/dlabel.lua:218 [/CODE]
That error is because you're defining A as a local variable, which is making A only exist in the AddButtons function. You need to do [CODE] local A, B [/CODE] At the top of your script, and then when defining them, just do [CODE] A = something B = something [/CODE] (That defines the local variables from before as something else)
[QUOTE=MPan1;50542845]That error is because you're defining A as a local variable, which is making A only exist in the AddButtons function. You need to do [CODE] local A, B [/CODE] At the top of your script, and then when defining them, just do [CODE] A = something B = something [/CODE] (That defines the local variables from before as something else)[/QUOTE] Thanks for the help man!
Sorry, you need to Log In to post a reply to this thread.