How to get this value on hud ?

I want to have the value of NumSlider on Screen but I don’t know how to assing it.

I know is with NumSlider:GetValue() but when I put it in AmmoHud() I get an error.

[lua]if CLIENT then

function GiveBullet()

local DermaPanel = vgui.Create( “DFrame” ) – Creates the frame itself
DermaPanel:SetPos( 100,200 ) – Position on the players screen
DermaPanel:SetSize( 200, 270 ) – Size of the frame
DermaPanel:SetTitle( " Ammo Suit V2" ) – Title of the frame

DermaPanel:SetDraggable( true ) – Draggable by mouse?
DermaPanel:ShowCloseButton( true ) – Show the close button?
DermaPanel:MakePopup() – Show the frame

function DermaPanel:Paint()
surface.SetDrawColor( 40, 40, 40, 170 )
surface.DrawRect( 0, 0, DermaPanel:GetWide(), DermaPanel:GetTall() )
end

local TestingPanel = vgui.Create( “DPanel”, DermaPanel )
TestingPanel:SetPos( 3, 20 )
TestingPanel:SetSize( 190, 1 )
TestingPanel.Paint = function() – Paint function
surface.SetDrawColor( 255, 255, 255, 255 ) – Set our rect color below us; we do this so you can see items added to this panel
surface.DrawRect( 0, 0, TestingPanel:GetWide(), TestingPanel:GetTall() ) – Draw the rect
end

local NumSlider = vgui.Create( “DNumSlider”, DermaPanel )
NumSlider:SetPos( 25,50 )
NumSlider:SetWide( 150 )
NumSlider:SetText( “Max Props” )
NumSlider:SetMin( 0 ) – Minimum number of the slider
NumSlider:SetMax( 256 ) – Maximum number of the slider
NumSlider:SetDecimals( 0 ) – Sets a decimal. Zero means it’s a whole number
NumSlider:SetConVar( “sbox_maxprops” ) – Set the convar
NumSlider.Paint = function() – Paint function
surface.SetDrawColor( 255, 255, 255, 250 ) – Set our rect color below us; we do this so you can see items added to this panel
surface.DrawRect( 0, 26, NumSlider:GetWide(), NumSlider:GetTall() - 30 ) – Draw the rect
end

end
concommand.Add (“AmmoAdd”, GiveBullet)

function AmmoHud()
local client = LocalPlayer()

draw.SimpleText(client:Health() “%”, “ScoreboardText”, 100, 50, Color(86, 104, 86, 255), 0, 0)

end
hook.Add(“HUDPaint”,“AmmoHud”, AmmoHud)

end[/lua]

You would draw NumSlider:GetValue()

yep I want to draw it on the hud

Your NumSlider variable is local to the GiveBullet function, therefore only accessible in that scope. You need to make it accessible to the AmmoHud function by declaring the local value outside of any function scope. Like this:

[lua]if CLIENT then

local NumSlider -- declare the variable outside of any function, this will make it available to the whole script
function GiveBullet()
	local DermaPanel = vgui.Create( "DFrame" )
	DermaPanel:SetPos( 100,200 )
	DermaPanel:SetSize( 200, 270 )
	DermaPanel:SetTitle( "                  Ammo Suit V2" )
	DermaPanel:SetDraggable( true )
	DermaPanel:ShowCloseButton( true )
	DermaPanel:MakePopup()
	function DermaPanel:Paint()
		surface.SetDrawColor( 40, 40, 40, 170 )
		surface.DrawRect( 0, 0, DermaPanel:GetWide(), DermaPanel:GetTall() )
	end
	
	local TestingPanel = vgui.Create( "DPanel", DermaPanel )
	TestingPanel:SetPos( 3, 20 )
	TestingPanel:SetSize( 190, 1 )
	TestingPanel.Paint = function()
		surface.SetDrawColor( 255, 255, 255, 255 )
		surface.DrawRect( 0, 0, TestingPanel:GetWide(), TestingPanel:GetTall() )
	end
	
	NumSlider = vgui.Create( "DNumSlider", DermaPanel )
	NumSlider:SetPos( 25,50 )
	NumSlider:SetWide( 150 )
	NumSlider:SetText( "Max Props" )
	NumSlider:SetMin( 0 )
	NumSlider:SetMax( 256 )
	NumSlider:SetDecimals( 0 )
	NumSlider:SetConVar( "sbox_maxprops" )
	NumSlider.Paint = function()
		surface.SetDrawColor( 255, 255, 255, 250 )
		surface.DrawRect( 0, 26, NumSlider:GetWide(), NumSlider:GetTall() - 30 )
	end
end
concommand.Add ("AmmoAdd", GiveBullet)

function AmmoHud()
	if NumSlider and NumSlider:GetValue() then
		draw.SimpleText(NumSlider:GetValue(), "ScoreboardText", 100, 50, Color(86, 104, 86, 255), 0, 0)
	end
end
hook.Add("HUDPaint","AmmoHud", AmmoHud)

end[/lua]

cool is working :slight_smile:

thanks MakeR

But when I close the panel I got this error
Should I set GetDeleteOnClose on false ?


Hook 'AmmoHud' Failed: lua\autorun\menuammo.lua:41: Tried to use invalid object (type Panel) (Object was NULL or not of the right type)


No, you just need to check if the panel is valid before calling GetValue.

[lua]if CLIENT then

local NumSlider -- declare the variable outside of any function, this will make it available to the whole script
function GiveBullet()
	local DermaPanel = vgui.Create( "DFrame" )
	DermaPanel:SetPos( 100,200 )
	DermaPanel:SetSize( 200, 270 )
	DermaPanel:SetTitle( "                  Ammo Suit V2" )
	DermaPanel:SetDraggable( true )
	DermaPanel:ShowCloseButton( true )
	DermaPanel:MakePopup()
	function DermaPanel:Paint()
		surface.SetDrawColor( 40, 40, 40, 170 )
		surface.DrawRect( 0, 0, DermaPanel:GetWide(), DermaPanel:GetTall() )
	end
	
	local TestingPanel = vgui.Create( "DPanel", DermaPanel )
	TestingPanel:SetPos( 3, 20 )
	TestingPanel:SetSize( 190, 1 )
	TestingPanel.Paint = function()
		surface.SetDrawColor( 255, 255, 255, 255 )
		surface.DrawRect( 0, 0, TestingPanel:GetWide(), TestingPanel:GetTall() )
	end
	
	NumSlider = vgui.Create( "DNumSlider", DermaPanel )
	NumSlider:SetPos( 25,50 )
	NumSlider:SetWide( 150 )
	NumSlider:SetText( "Max Props" )
	NumSlider:SetMin( 0 )
	NumSlider:SetMax( 256 )
	NumSlider:SetDecimals( 0 )
	NumSlider:SetConVar( "sbox_maxprops" )
	NumSlider.Paint = function()
		surface.SetDrawColor( 255, 255, 255, 250 )
		surface.DrawRect( 0, 26, NumSlider:GetWide(), NumSlider:GetTall() - 30 )
	end
end
concommand.Add ("AmmoAdd", GiveBullet)

function AmmoHud()
	if NumSlider and NumSlider:IsValid() then -- check if the numslider is valid
		draw.SimpleText(NumSlider:GetValue(), "ScoreboardText", 100, 50, Color(86, 104, 86, 255), 0, 0)
	end
end
hook.Add("HUDPaint","AmmoHud", AmmoHud)

end[/lua]

Good! , I think I understand better thanks alot

But If I want to make the value appear permanently on screen what do I do ?
It’s because when I close the panel the Value disappear too.

You have to store the value in a variable whenever it changes and use that variable instead of calling GetValue.

[lua]if CLIENT then

local NumSliderValue = 0; -- store the NumSlider value in this variable when it is changed
function GiveBullet()
	local DermaPanel = vgui.Create( "DFrame" )
	DermaPanel:SetPos( 100,200 )
	DermaPanel:SetSize( 200, 270 )
	DermaPanel:SetTitle( "                  Ammo Suit V2" )
	DermaPanel:SetDraggable( true )
	DermaPanel:ShowCloseButton( true )
	DermaPanel:MakePopup()
	function DermaPanel:Paint()
		surface.SetDrawColor( 40, 40, 40, 170 )
		surface.DrawRect( 0, 0, DermaPanel:GetWide(), DermaPanel:GetTall() )
	end
	
	local TestingPanel = vgui.Create( "DPanel", DermaPanel )
	TestingPanel:SetPos( 3, 20 )
	TestingPanel:SetSize( 190, 1 )
	TestingPanel.Paint = function()
		surface.SetDrawColor( 255, 255, 255, 255 )
		surface.DrawRect( 0, 0, TestingPanel:GetWide(), TestingPanel:GetTall() )
	end
	
	local NumSlider = vgui.Create( "DNumSlider", DermaPanel ) -- This can be local to the GiveBullet function again
	NumSlider:SetPos( 25,50 )
	NumSlider:SetWide( 150 )
	NumSlider:SetText( "Max Props" )
	NumSlider:SetMin( 0 )
	NumSlider:SetMax( 256 )
	NumSlider:SetDecimals( 0 )
	NumSlider:SetConVar( "sbox_maxprops" )
	NumSlider.Paint = function()
		surface.SetDrawColor( 255, 255, 255, 250 )
		surface.DrawRect( 0, 26, NumSlider:GetWide(), NumSlider:GetTall() - 30 )
	end
	NumSlider.ValueChanged = function()
		NumSliderValue = NumSlider:GetValue()
	end
end
concommand.Add ("AmmoAdd", GiveBullet)

function AmmoHud()
	if NumSliderValue then 
		draw.SimpleText(NumSliderValue, "ScoreboardText", 100, 50, Color(86, 104, 86, 255), 0, 0)
	end
end
hook.Add("HUDPaint","AmmoHud", AmmoHud)

end[/lua]

Wow,
I think I can start on this base to experiment GetValue

Many Thanks !!