• How to send a client the value of a variable with SendLua
    12 replies, posted
First of all, I do not want to us net messages for this. Example: [CODE] local num = math.random( 100000 ) ply:SendLua( num ) [/CODE] Obviously this will error on the client saying that the variable 'num' is not defined. Is there anyway to send the client the actual number values on num via SendLua?
Don't use SendLua, use a net message. I don't see why you wouldn't want to.
ply:SendLua("num = "..num)
[QUOTE=MeepDarknessM;44473390]ply:SendLua("num = "..num)[/QUOTE] That doesn't work.
not helpful info -1
really easy alternative way to send the value of a variable of any type... (though it is kinda wasteful) -- SERVER [CODE] util.AddNetworkString("messagename"); function sendvar( pl, val ) net.Start( "messagename" ); net.WriteTable( { val } ); net.Send( pl ); end [/CODE] -- CLIENT [CODE] net.Receive( 'messagename', function() local var = net.ReadTable()[1]; -- do something with it... end); [/CODE] SendLua just shouldn't be used for this flat out should not. There is something seriously wrong with your conception of programming if your trying to use SendLua to send a value when it is intended for dynamic code. That having been said and though this makes me want to cry... -- SERVER [CODE] function SendValueToPlayer( pl, num ) pl:SendLua( 'ThisIsRlyStupid('..num..')' ); end [/CODE] -- CLIENT [CODE] function ThisIsRlyStupid( num ) -- do stuff with it... end [/CODE] as i said though this is really pretty much the stupidest thing you could do... well actually I wouldn't put it past you to find something stupider if you think this is a good idea.
[QUOTE=MeepDarknessM;44473479]not helpful info -1[/QUOTE] What? It doesn't work. It simply errors flat out with undefined variable. What more information do you want? [QUOTE=thelastpenguin;44473539] There is something seriously wrong with your conception of programming if your trying to use SendLua to send a value when it is intended for dynamic code. as i said though this is really pretty much the stupidest thing you could do... well actually I wouldn't put it past you to find something stupider if you think this is a good idea.[/QUOTE] I apologize for upsetting you with my question. I like to come to this board in order to learn things about a programming language that I have no where near achieved mastery. I'm still very new to Lua compared to many of the regulars around here, and I very much enjoy learning from them. If you want more information, I'm looking at writing a simple anticheat for myself as a little project to help improve. I figure a good place to start would be to figure out how to get the entire clientside code of the anticheat to the client without saving it to the cache in order to slow down reverse engineering. Obviously this would be done through net messages and the use of RunStringEx. However, that would involve putting hard-coded clienside code on the client, where they could see the name of a net message and detour it. So what I'm trying to do is to create a dynamically-named net message so that the client cannot predict the end user cannot predict the name, while still having the net message loaded into his Lua Environment. If you don't have the knowledge to do this, or think that it's impossible, both of those things are perfectly valid and I accept that. However, I don't understand why you felt the need to harass me for asking a question. edit: Yes I understand that they can still use C++ modules to detour RunStringEx and there's nothing I can do about that. However, it is possible to use debug.getinfo in order to use the original C function of both net.Receive, net.ReadString, and RunString ex, so I'm not particularily worried about any Lua Detours.
[lua]function(ply, num) ply:SendLua( [[print( ]].. num .. [[ ) ]]) end[/lua] 99% sure this would work
[QUOTE=zerothefallen;44473718][lua]function(ply, num) ply:SendLua( [[print( ]].. num .. [[ ) ]]) end[/lua] 99% sure this would work[/QUOTE] wow it does, thank you very much! edit: If you have the time, would you mind explaining why it works? I don't see why the [[ and ]] operators are any different than the ' or " operators in this case, but apparently they are.
[QUOTE=CallMePyro;44473759]wow it does, thank you very much![/QUOTE] np /solved [editline]7th April 2014[/editline] [QUOTE=CallMePyro;44473759] edit: If you have the time, would you mind explaining why it works? I don't see why the [[ and ]] operators are any different than the ' or " operators in this case, but apparently they are.[/QUOTE] [[ ]] is for lua gernally [lua]function runlua(ply, lua) RunStringEx(lua, ply:Name()) end runlua(ent[1], [[print("HELLOw")]])[/lua] you cant exactly use lua like a string using " " because of formatting When you want to run lua, but run it dynamically for different values, you can use these. [lua]function runlua(ply) x = 10 y = 10 z = -20 ply:SendLua([[print(]] .. x + y + z .. [[ ) ]]) end -- Client prints 0[/lua] edit: Asked a friend, I was sorta close, they're actually multi-line strings. ideal for lua, otherwise.
Interesting. So I've noticed that if I use the function you provided, a simple print function, I get some (seemingly) odd results [CODE] local val = 100; BroadcastLua( [[print(]].. val .. [[)]] ) [/CODE] it prints the value correctly(also works with floats). However, if I change the value to a string, it prints nil. Why is this?
[QUOTE=CallMePyro;44473942]Interesting. So I've noticed that if I use the function you provided, a simple print function, I get some (seemingly) odd results [CODE] local val = 100; BroadcastLua( [[print(]].. val .. [[)]] ) [/CODE] it prints the value correctly(also works with floats). However, if I change the value to a string, it prints nil. Why is this?[/QUOTE] [["string"]] is invalid [[ ]] is multi line strings, [lua]local val = hello; BroadcastLua( [[print(]].. val .. [[)]] )[/lua] This would work, fairly sure. No quotes around the string [editline]7th April 2014[/editline] redundancy tl;dr
[QUOTE=zerothefallen;44473955][["string"]] is invalid [[ ]] is multi line strings, [lua]local val = hello; BroadcastLua( [[print(]].. val .. [[)]] )[/lua] This would work, fairly sure. No quotes around the string[/QUOTE] What you suggested didn't work, but this did: local val = [["hello"]] BroadcastLua( [[print(]].. val .. [[)]] ) Seems like I've got it all worked out. Thanks a bunch!
Sorry, you need to Log In to post a reply to this thread.