Need assistance with "player:SendLua()".

Hey, i cant really understand the “player:SendLua()” function so i’ve got a few questions:
How do i send a quoted function? (wiki didn’t really explained it that well) Example:


ply:SendLua("self:EmitSound("/../.wav")")

How do i send a whole table through it?

And when im using:


function Example() self:EmitSound("/../.wav") end
function ENT:Think() ply:SendLua("Example()") end

why do get this error?


:1: attempt to call global 'Example' (a nil value)

Also… Its being ran SERVERside (init.lua)

You would send a table by doing

ply:SendLua(“a = {1, 2, 3}”)

which would set the global variable a to {1, 2, 3}.

You get that error because there’s no global function called Example on the client lua state.

What if i want to send this kind of table?


local Table = {}
Table.string = "String"
Table.boolean = true
Table.float = 0.5

Also. How would i go around the error?

[lua]
ply:SendLua(“Table = glon.decode(”"…glon.encode(Table)…"")")
[/lua]

Though this is a horrible way of doing it. Learn to use usermessages instead.

Also don’t make locals with SendLua, you won’t be able to reference the variable except inside the one SendLua you declare it in.

SendLua sends a piece of Lua code to a player so that they run it clientside.

Clientside.


function Example() self:EmitSound("/../.wav") end
function ENT:Think() ply:SendLua("Example()") end

Not only your Example function is horribly wrong, but it’s also declared serverside, so it does not exist clientside.

You would have this clientside, for instance in cl_init.lua

[lua]function Example()
LocalPlayer():EmitSound("/…/.wav")
end[/lua]

And this serverside

[lua]ply:SendLua(“Example()”)[/lua]

If you want the server to send data to a player or run clientside functions, SendLua is not what you need.

This is what you need:

So basicly what im trying to do is create a hook on a specified player from a sent. ‘HUDPaint’ in this case.

I disagree, I don’t see any harm in using SendLua to run functions on a player. Transferring data is not a good thing to use SendLua for though.

So what would be the best way to execute a HUDPaint hook on a player?

If you’re going to create a function, just make the function in a clientside file, then you can add the hook with

ply:SendLua(“hook.Add(‘HUDPaint’, ‘uid’, functionName)”)

Thanks, ill do that.

SendLua is still bad habit though. You’re sending a string of code when you can put that code in a usermessage hook clientside and just send an empty message so it gets called.

I haven’t exactly used an umsg before… So, how exactly would be able to use it in this situation?

Serverside.
[lua]-- If you’re planning to send it often, this will make it lighter to send.
– umsg.PoolString(“My message”);

local function send(ply)
umsg.Start(“My message”, ply);
umsg.End();
end

send(Entity(1));[/lua]
Clientside.
[lua]usermessage.Hook(“My message”, function()
print(“Hi”);
end);[/lua]