Running a function on the server via client?

If i have a server function, how do i get the client to run the function?
In my example, i have the GUI set up on the clients side, then in the DButton.DoClick = function() part, what should i put?
I tried putting RunConsoleCommand(“os_checkuser”) but to me it seems kinda wrong to do it like this, is there a better way?
Also, the function i want to run on the server returns true or false, how would i get the client to recieve the true or false?

Hope this makes sense, thanks in advance! :slight_smile:

Have you tried creating whatever function clientside? Have you tried making it shared?

Only way to send data/function calls to the server is via concommands. To send data back to the client, you can use either concommands or usermessages. But it won’t be instantaneous. You will need to freeze/disable your menu until you get the data back from the server.


What is up with me… I always seem to be the second to reply in the last few days.

The function is server only, it basically opens a file and checks for a username, but i don’t want this function being shown to the client.
Actually, say if the function was on the client, how would i call it anyway? That is also something i wanna know :stuck_out_tongue:

You can’t. Do what Nevec said. Call it with a console command and return a usermessage.

Wow, so there is no way to just call a function even if it is on the same side (client) and even declared in the same file etc? Lua is funny.
Thanks for the help everyone, greatly clears things up knowing this :slight_smile:

Well if the function is clientside, it can be called clientside. Whom the function can be run on depends by the file you wrote it in.

You call functions by basically copying their declaration. Like so…

// define
function MyFunc( ) print( “MyFunc” ); end

// call
MyFunc( );

You can also call a function with arguments, like so…

function MyFunc( a, b ) print( a, b ); end

MyFunc( “L”, “ua” );

And functions can return values as well…

function MyFunc( ) return “Lu”, “a”; end

local a, b = MyFunc( );
print( a, b );

But be careful, because definition and calling order matters…

– atempt to call global ‘MyFunc’, a nil value…
– or some other error like that
MyFunc( );

function MyFunc( ) end

And if the third and forth examples confused you, functions can take arguments and return them all together…

function MyFunc( a, b ) return a + b; end

print( MyFunc( 1, 2 ) );

Ahh, Nevec that post was very useful, thanks! Lua scripts take so long to test and debug compared to Java, Visual Basic, Python etc because you need to load up Garrys Mod, it is the reason i am learning so slowly hah.

You can reload scripts using the ‘lua_openscript’ and ‘lua_openscript_cl’ concommands, as well as you can execute lua code directly using ‘lua_run’ and ‘lua_run_cl’.

Ah yeah i forgot about those O_O

Another small question, if you declare a local variable within a function, is it only available within that function or within the file it is declared in?

Only the function.

And reloading the map reloads all addons and the gamemode. However it won’t load addons you added later, you need to restart the server for that.