Dbutton help

Hi ive been having an issue with dbuttons where i want to add a convar
I want to make it so once button is clicked the text goes green which ive done
and make it stay green when a convar is turned on for example example_test 1
and when you press the button again the green button turns back to white so example_test 0

would anyone have any idea on how to do this?


local derma1 = vgui.Create( "DFrame" )
derma1:SetPos( 5, 5 )
derma1:SetSize( 300, 300 )
derma1:SetTitle( "Name window" )
derma1:SetVisible( true )
derma1:SetDraggable( false )
derma1:ShowCloseButton( true )
derma1:MakePopup()

local DPanel = vgui.Create( "DPanel", derma1 )
DPanel:SetPos( 10, 30 ) 
DPanel:SetSize( 125, 35 ) 
DPanel.Paint = function(s , w , h)
   
        draw.RoundedBox(4,0,0,w , h,Color(50, 50, 100, 255))
end

local DButton = vgui.Create( "DButton", DPanel )
DButton:SetPos( 5, 5 )
DButton:SetText( "single" )
DButton:SetSize( 115, 25 )
DButton:SetTextColor( Color( 255, 255, 255, 255 ) )
DButton.Paint = function(s , w , h)
   
        draw.RoundedBox(4,0,0,w , h,Color(5, 5, 40, 255))
end

DButton.OnCursorEntered = function()
	print( "You hovered over this button" )
	DButton:SetTextColor( Color( 255, 255, 0, 255 ) )	
end
DButton.OnCursorExited = function()
	print( "You left the button area" )
	DButton:SetTextColor( Color( 255, 255, 255, 255 ) )
end
DButton.DoClick = function()
	print( "Button was clicked!" )
	surface.PlaySound("UI/buttonclick.wav")
	DButton:SetTextColor( Color( 0, 255, 0, 255 ) )
end


i added a derma so its easier to work with.
dont mind the button .DoClick and .OnCursorExited print strings i did that cause i wanted to test shit lol :stuck_out_tongue:

I would like to also know if someone could make the same thing but as a dcombo box with the same colors at the button above ^

Could do it like this,

[lua]
local DButton = vgui.Create(“DButton”, DPanel)
DButton:SetPos(5, 5)
DButton:SetSize(115, 25)
DButton:SetText("")

DButton.Paint = function(s, w, h)
draw.RoundedBox(4, 0, 0, w, h, Color(5, 5, 40, 255))
draw.SimpleText(“Single”, “TargetID”, w / 2, h / 2, GetConVarNumber(“example_test”) == 1 and color_green or color_white, 1, 1)
end

DButton.DoClick = function()
if GetConVarNumber(“example_test”) >= 1 then
LocalPlayer():ConCommand(“example_test 0”)
else
LocalPlayer():ConCommand(“example_test 1”)
end
end
[/lua]

Theres simpler approaches which doesnt include setting a convar.

You should consider making it a DCheckBox and using

Panel:SetConVar

Nah wont work ive tried and cant seem to make a simple button to make a convar go from example_test 1 to example_test 2 on the same button :confused:

Ive used this but when i do this multiple times it stuffs up my custom dpanel boxes ive made only works for 1 button for some reason :confused:


local toggle = false

local derma1 = vgui.Create( "DFrame" )
derma1:SetPos( 5, 5 )
derma1:SetSize( 300, 300 )
derma1:SetTitle( "Name window" )
derma1:SetVisible( true )
derma1:SetDraggable( false )
derma1:ShowCloseButton( true )
derma1:MakePopup()
 
local DPanel = vgui.Create( "DPanel", derma1 )
DPanel:SetPos( 10, 30 )
DPanel:SetSize( 125, 35 )
DPanel.Paint = function(s , w , h)
   
        draw.RoundedBox(4,0,0,w , h,Color(50, 50, 100, 255))
end
 
local DButton = vgui.Create( "DButton", DPanel )
DButton:SetPos( 5, 5 )
DButton:SetText( "single" )
DButton:SetSize( 115, 25 )
DButton:SetTextColor( Color( 255, 0, 0, 255 ) )
DButton.Paint = function(s , w , h)
   
        draw.RoundedBox(4,0,0,w , h,Color(5, 5, 40, 255))
end
 
DButton.OnCursorEntered = function()
    print( "You hovered over this button" )
    DButton:SetTextColor( Color( 255, 255, 0, 255 ) )  
	
	if toggle then
		DButton:SetTextColor( Color( 100, 255, 100, 255 ) )
	else
		DButton:SetTextColor( Color( 255, 100, 100, 255 ) )
	end
end
DButton.OnCursorExited = function()
    print( "You left the button area" )
	
	if toggle then
		DButton:SetTextColor( Color( 0, 255, 0, 255 ) )
	else
		DButton:SetTextColor( Color( 255, 0, 0, 255 ) )
	end
end
	
DButton.DoClick = function()
	toggle = !toggle
	
	if toggle then
		GetConVar("xeno_aimbot_toggle"):SetInt(1)
	else
		GetConVar("xeno_aimbot_toggle"):SetInt(0)
	end
	
    print( "Button was clicked!" )
    surface.PlaySound("UI/buttonclick.wav")
	
	if toggle then
		DButton:SetTextColor( Color( 100, 255, 100, 255 ) )
	else
		DButton:SetTextColor( Color( 255, 100, 100, 255 ) )
	end
end

This only works for 1 button because the only button you are applying those functions to are DButton.

That’s exactly what the example does on the SetConVar page. You did not try at all. The only person you’re fooling is yourself.

Yeah my bad i tried it on another derma so see if it could work but it doesn’t switch between the 2 convars if i were to make more buttons i need to make more = toggle such as

1st button = toggle
2nd button = toggle 2
ext

it works and i have solved my problem, thank you anyway :*