• Lua Gmod Need help Array (server to client)
    17 replies, posted
Hellow guys so i have one question.Can you send an array from server to client? if you how?
Can you please elaborate on your question?
i want to transsfering data from server to client ( with my array)
[QUOTE=barz743;44674981]i have an array with information on the server i want to use this information(array) on my client. (i want to send this array from server to client)[/QUOTE]Unless someone else can answer your question from what you've said, I personally can't. Can you please give an example or something? I understand that you have an array and that you want to use it on the client but what exactly are you trying to do?
Check for the net library [lua] //Server util.AddNetworkString("SendMyOrder") //This will avoid an unpolled message function createOrder(ply) local tbl = {"Cheese",42,true} //A table with any kind of data net.Start("SendMyOrder") //We start our interaction with the client net.WriteTable(tbl) //We write our table for send it net.Send(ply) //We send this table to the player ply end //Client net.Receive("SendMyOrder",function(len) local tbl = net.ReadTable() //We assign the table that we wrote before PrintTable(tbl) //Will print cheese,42 and true end) [/lua]
Info[1] = ply:name info[2] = text info[3] = number (1) info[4] = color I want to trasfer this deta from my server to my client(how can i do it)
[QUOTE=barz743;44675000]Info[1] = ply:name info[2] = text info[3] = number (1) info[4] = color I want to trasfer this deta from my server to my client(how can i do it)[/QUOTE]Go with using the net library, like what gonzalolog said.
Use net.SendToServer( ); from client to server, use net.Send( Player ); to send to one player from the server, or net.Broadcast( ); to send to all players from server. To read and write a table, regardless of whether its from client to server or server to client, you'll use net.WriteTable( tableReferenceVariableGoesHere ); and to read it from the receiving side: local _myTable = net.ReadTable( ); Edit: Whoops, had this question open a little too long before responding...
Use the net library; [lua]//SERVERSIDE STUFF util.AddNetworkString( 'SendTable' ) -- Pool the net name so it can actually be sent function TableToClient( pl, text, number, color ) -- Customise your function local Info = { pl:Name(), text, number, color } -- Your table net.Start( 'SendTable' ) -- Start the net message net.WriteTable( Info ) -- Write the table into the net message net.Send( pl ) -- Send it to a specific player end //CLIENTSIDE STUFF function ReceiveNet() local tbl = net.ReadTable() -- Read the table sent from the server for _, v in pairs( tbl ) do chat.AddText( Color(255,255,255), v .. ' read from server' ) -- This is just to debug it end end net.Receive( 'SendTable', ReceiveNet )[/lua]
Ty every one!! I got one more problem, Do you guys know how to make a lua script 'wait', in clientside? I need a command to executed 5 seconds after a function. [editline]29th April 2014[/editline] Hey guys.. Apperantly i got an error on my code, it is when i try and read the table i sent. I am getting this error: [ERROR] lua/includes/modules/net.lua:136: attempt to concatenate local 'typeid' (a nil value) 1. ReadType - lua/includes/modules/net.lua:136 2. ReadTable - lua/includes/modules/net.lua:84 [B]3. v - addons/worldsay/lua/autorun/client/cl_client.lua:3[/B] 4. unknown - lua/includes/modules/hook.lua:84 on this code: [CODE] local Value = net.ReadTable() [/CODE]
Could you post the function containing that code and the net.Receive that's calling the function?
[url]http://pastebin.com/UPr0vbki[/url]
It's because you're trying to do net.ReadTable() when the hook calls the function, and there's nothing to read since no net message has been received when the hook is called, you need to ensure the table is set before the hook is called, meaning you'd need to wrap the code within the function in an if statement, checking if the net.ReadTable actually returns anything. [lua] local Value = {} function Draw( ply ) if ( net.ReadTable() ) then Value = net.ReadTable() local offset = Vector( 0, 0, 85 ) local ang = LocalPlayer():EyeAngles() local pos = Value[1] + offset + ang:Up() ang:RotateAroundAxis( ang:Forward(), 90 ) ang:RotateAroundAxis( ang:Right(), 90 ) cam.Start3D2D( pos, Angle( 0, ang.y, 90 ), 0.25 ) draw.DrawText( Value[2], "ChatFont", 2, 2, Value[3], TEXT_ALIGN_CENTER ) cam.End3D2D() end end net.Receive( 'ChatColor', Draw ) hook.Add( "PostPlayerDraw", "DrawName", Draw ) [/lua] This should work in theory, but I've not tested it. There's probably an easier way to do it, like checking if a net message has actually been received by a specific name.
First of all Thanks! But when i look at my player, I am getting this error for some reason: [ERROR] lua/includes/modules/net.lua:136: attempt to concatenate local 'typeid' (a nil value) 1. ReadType - lua/includes/modules/net.lua:136 2. ReadTable - lua/includes/modules/net.lua:84 3. v - addons/worldsay/lua/autorun/client/cl_client.lua(cat) 4. unknown - lua/includes/modules/hook.lua:84
[QUOTE=barz743;44675676]First of all Thanks! But when i look at my player, I am getting this error for some reason: [ERROR] lua/includes/modules/net.lua:136: attempt to concatenate local 'typeid' (a nil value) 1. ReadType - lua/includes/modules/net.lua:136 2. ReadTable - lua/includes/modules/net.lua:84 3. v - addons/worldsay/lua/autorun/client/cl_client.lua(cat) 4. unknown - lua/includes/modules/hook.lua:84[/QUOTE]Did LuckyLuke not just answer that question? You just asked the same question again...?
-snip- the code is terrible and won't work
[QUOTE=barz743;44675676]First of all Thanks! But when i look at my player, I am getting this error for some reason: [ERROR] lua/includes/modules/net.lua:136: attempt to concatenate local 'typeid' (a nil value) 1. ReadType - lua/includes/modules/net.lua:136 2. ReadTable - lua/includes/modules/net.lua:84 3. v - addons/worldsay/lua/autorun/client/cl_client.lua(cat) 4. unknown - lua/includes/modules/hook.lua:84[/QUOTE] Is that using the code I just provided?
Here's an example of how to have the client render / display something for X seconds within a hook, then remove itself after. The example sends a net message to the client when they connect, the client receives it and adds a hook to render and sets the time it started, when the time expires within the hook it is removed. I'd recommend separating them out just a bit because the net.ReadTable( ) will only exist for 1 frame... [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/display_something_on_initial_spawn.lua.html[/url] Notice on the client-net-receive; it adds a hook that manages itself. That's how something like that should be done ( rather, its one way to properly execute it )..
Sorry, you need to Log In to post a reply to this thread.