How to run client side code!

Hi all, Ive been working with lua (im just starting off)

Running code from serverside is easy, and can be done in the server console, but how do i run client side code FROM server side ( for guis and stuff )

A nice link or explanation would be much appreciated, thank you!

Hi. I recommend you to take a look at usermessages. This video gives you a good overview of how it works:

Inside a gamemode, or addon, or are you just trying to send lua to clients?

AddCSLuaFile( file ) somewhere serverside, and then somewhere clientside, include( file )

If sending lua, Player.SendLua

If you want to quickly add a clientside script to your server, you can just place it in lua/autorun/. At the top of the file, add
[lua]if SERVER then AddCSLuaFile() return end[/lua]

This sends the file to the client, and stops the server from going any further.

That is not how you run code on the client.
Maybe you should learn how to use things… say, maybe… the net library(http://www.facepunch.com/showthread.php?t=1228086)

Hey all, thanks for your replies, ill see how i go!

EDIT: I tried out SendLua and it works! How ever…

[Lua] ply:SendLua ( ‘local DermaPanel = vgui.Create( “DFrame” ) ; DermaPanel:SetPos( 50,50 ); DermaPanel:SetSize( 1000, 900 ) ; DermaPanel:SetTitle( “Testing Derma Stuff” ) ; DermaPanel:SetTitle( “Testing Derma Stuff” )’ ) [/Lua]

This might become tedious to write, ill try and concatting strings or make a function to make it easier

thanks anyway!

Hi all again, im not sure if you guys are still looking at this thread, but im having trouble trying to get code run on the client side

How do i work ‘AddCsLuaFile()’ exactly, ive looked up various ways and none of them work

[Lua]if SERVER then

-- Make sure the clients downloads this file
AddCSLuaFile ( "ragdollAnimate_v2.lua" )

end

if CLIENT then
local DermaPanel = vgui.Create( “DFrame” ) – Creates the frame itself
DermaPanel:SetPos( 50,50 ) – Position on the players screen
DermaPanel:SetSize( 1000, 900 ) – Size of the frame
DermaPanel:SetTitle( “Testing Derma Stuff” ) – Title of the frame
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( true ) – Draggable by mouse?
DermaPanel:ShowCloseButton( true ) – Show the close button?
DermaPanel:MakePopup() – Show the frame
end [/Lua]

Just some simple code to make a gui on a client, what would i have to modify or do?

What you need to do is write lua files that contain code the client should execute and have the server send make the clients download them with AddCSLuaFile(filename).

Once that’s done include your new clientside files from cl_init.lua just like you include serverside files from init.lua.

Hey, thanks for the advice! The clientside scripts dont seem to be running, ive tried printing stuff out from the client but nothing happens :\

init.lua
[Lua] – Init File, entirely Serverside

AddCSLuaFile ( “shared.lua” )
AddCSLuaFile ( “cl_init.lua” )

include ( ‘shared.lua’ ) [/Lua]

(Theres nothing in shared.lua)

cl_init.lua
[Lua]
– cl_init - clientside

include ( ‘shared.lua’ )

MsgN ( "THIS IS SPARTA!!!
" )

DermaPanel = vgui.Create( “DFrame” ) – Creates the frame itself
DermaPanel:SetPos( 50,50 ) – Position on the players screen
DermaPanel:SetSize( 1000, 900 ) – Size of the frame
DermaPanel:SetTitle( “Testing Derma Stuff” ) – Title of the frame
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( true ) – Draggable by mouse?
DermaPanel:ShowCloseButton( true ) – Show the close button?
DermaPanel:MakePopup() – Show the frame[/Lua]

Why are you
'ing a MsgN?

Ye woops, its meant to be Msg, my bad, but i think i tried Msg, and that didnt work.

What also isnt working is the gui still aint popping up, can you guys please look at my code :\

EDIT: Ive done various testing and figured out that both init.lua and shared.lua are running, but not cl_init.lua

BTW, i am using lua_openscript to run init.lua ( which i hope will also run cl_init.lua on the client )

It won’t. cl_init is run as it’s said, it’s run when the game/entity/gamemode is initialized. Where is your files? Is this a gamemode, entity or is it in the lua/ folder? Use lua_openscript to run the init, then use lua_openscript_cl to run the cl_init, then use both openscript to run the shared.
But if you put the files in lua/autorun/, lua/autorun/client and lua/autorun/server you can have them initialize on game start (they don’t need to be named anything special then), and you can just change map in order to have them run, which goes pretty fast now with the decreased loadingtimes.

Thanks Donkie, im just running them from the lua folder using lua_openscript, but i will try the autorun.
If that doesnt work, Ill run it as a gamemode