Derma Menu Slider Values?

Hi, if you seen my other thread, you may know I am creating a automatic clear decals+freeze props addon.

Anyway, here is my issue.

I am not that good with lua but here goes,

In my Derma Menu, I have a DNumSlider, the value is representing seconds


local Slider = vgui.Create( "DNumSlider", frame )
Slider:SetPos( 70, 200 )
Slider:SetSize( 300, 100 )
Slider:SetText( "Cleanup Every" )
Slider:SetMin( 5 )
Slider:SetMax( 3600 )
Slider:SetDecimals( 0 )

What I am trying to achieve, is that the clientside derma menu slider can change the value of the ConVar I created:


CreateConVar( "AutoULXMenu_Timer", 600, { FCVAR_ARCHIVE } )

and with that convar value,

I want to be able to get the value and use it on the server script:


timer.Modify("AutoULXFreezeProps", **CONVAR VALUE HERE**,0, function() game.ConsoleCommand( "ulx Freezeprops
" ) end)

How is the best way to get the clientside slider to be able to change the convar value, and the convar value to be used in the server side timer?

Thanks Alot

DNumSlider has a SetConVar function that you can use. Example 1 on wiki

Then you can use a net message to tell the server to update the timer with the new convar value:



-- Server
util.AddNetworkString( "slider_update_net" )

net.Receive( "slider_update_net", function( len, ply )
	if ( ply:IsAdmin() ) then -- Check if the player is an admin or w/e
		local yourconvar = GetConVar( "yourconvarname" )
		-- Update your timer here with yourconvar:GetInt()
	end
end )

-- Client
Slider.OnValueChanged = function( n )
	net.Start( "slider_update_net" )
	net.SendToServer()
end


Thanks! This helped a lot, though I’ve just come across a problem.

Not too sure how to set the CVAR up.

I need it to be on the server, and the clients to be able to change the value of the CVAR.

Currently this is how it is set up on the server:


CreateConVar( "AutoULXMenu_Timer", 600, { FCVAR_ARCHIVE, FCVAR_REPLICATED, FCVAR_USERINFO } )

The server recognises it, but the client shows that it doesn’t exist. Though I added the Replicated and User Info flags…


The previous set up I had was putting the CVAR with Archive on both client and server luas, but I realised that they are both acting as seperate cvars, and the server one wasn’t changing alongside the client one

Well, I don’t think you’ll beable to have the client mod the convar directly on their end and have it update, directly on the console(without rcon command of course), so you need to take the net approach.

I suggest adding an “Apply” button at the bottom of your panel (below the slider), so that you don’t spam the server with requests while sliding the numslider around. so:



--Client
function applyButton:DoClick()
	net.Start("update_ulx_convar");
	net.WriteUInt(slider:GetValue(), 32);
	net.SendToServer();
end


Then on the Server:



--Server
util.AddNetworkString("update_ulx_convar");
net.Receive("update_ulx_convar", function(size, ply)
	if (ply:IsSuperAdmin()) then --Make sure the player can even send this message. Change this to what ever you need
		RunConsoleCommand("AutoULXMenu_Timer", net.ReadUInt(32));
		ply:ChatPrint("Convar updated..");
		timer.Modify("AutoULXFreezeProps", GetConVar("AutoULXMenu_Timer"):GetInt() ,0, function() game.ConsoleCommand( "ulx Freezeprops
" ) end)
	end
end)


Thanks.

I’ve added a new button:


local buttonset = vgui.Create("DButton" , frame)
buttonset:SetPos(400,468)
buttonset:SetSize(45,30)
buttonset:SetText('Apply')

and then the fuction for it:


function buttonset:DoClick()
	net.Start("update_ulx_convar");
	net.WriteUInt(Slider:GetValue(), 32);
	net.SendToServer();
end

However once the slider is moved, and Apply is pressed, it gives:



[ERROR] lua/vgui/dnumslider.lua:135: attempt to index field 'Scratch' (a nil value)
  1. GetValue - lua/vgui/dnumslider.lua:135
   2. DoClick - addons/autoulxcleaner/lua/autorun/client/timerscript.lua:56
    3. unknown - lua/vgui/dlabel.lua:218


in the client console.

Can you post the entirety of the relevant code? Most importantly the parts where you create the slider and do anything with it

Clientside -

SLIDER:


local Slider = vgui.Create( "DNumSlider", frame )
Slider:SetPos( 70, 200 )
Slider:SetSize( 300, 100 )
Slider:SetText( "Cleanup Every" )
Slider:SetMin( 5 )
Slider:SetMax( 3600 )
Slider:SetDecimals( 0 )

Apply/Set Button:


local buttonset = vgui.Create("DButton" , frame)
buttonset:SetPos(400,468)
buttonset:SetSize(45,30)
buttonset:SetText('Apply')	

function buttonset:DoClick()
	net.Start("update_ulx_convar");
	net.WriteUInt(Slider:GetValue(), 32);
	net.SendToServer();
end


Serverside -

Convar


CreateConVar( "AutoULXMenu_Timer", 600, { FCVAR_ARCHIVE, FCVAR_REPLICATED, FCVAR_USERINFO } )

Net.


util.AddNetworkString( "update_ulx_convar" )


net.Receive("update_ulx_convar", function(size, ply)
	if (ply:IsAdmin()) then
		RunConsoleCommand("AutoULXMenu_Timer", net.ReadUInt(32));
		ply:ChatPrint("Convar updated..");
		timer.Adjust("AutoULXNotifcation", GetConVar("AutoULXMenu_Timer"):GetInt(),0, function() BroadcastLua("chat.AddText(Color(0,255,0), 'AutoULXCleaner ', Color(255,255,255), '- ', Color(82,82,82), 'Props Frozen + Decals Cleared!')") end)
		timer.Adjust("AutoULXFreezeProps", GetConVar("AutoULXMenu_Timer"):GetInt(),0, function() game.ConsoleCommand( "ulx Freezeprops
" ) end)
		timer.Adjust("AutoULXClearDecals", GetConVar("AutoULXMenu_Timer"):GetInt(),0, function() game.ConsoleCommand( "ulx cleardecals
" ) end)
		
	end
end)


That’s quite the odd error. It implies that the slider did not initialize properly.

Scratch is created in the Init function of the slider, as shown below:



	self.Scratch = self.Label:Add( "DNumberScratch" )
		self.Scratch:SetImageVisible( false )
		self.Scratch:Dock( FILL )
		self.Scratch.OnValueChanged = function() self:ValueChanged( self.Scratch:GetFloatValue() )


But the numslider works just fine? You can drag the slider around and stuff and the displayed value updates?

Yeah, I added a concommand to print the value of the slider, which works fine. Shows the same value as the slider.

I am just gonna try and revert to a sort of drop down box options having main numbers, eg 1 minute, 5 minutes, 10 minutes etc etc, might not be as interactive as a slider, but if the slider won’t work that I’ll have to use a different method.

Oh Well!

Rather than using the slider, I changed and used the drop down menu… Works normally now.