LUA Garry's mod quick way to transfer data

I’m wondering what’s the easiest and most efficient way to transfer data to all clients.
Basically what my script is doing is adding data to tables, now all i want to do is when a method is loaded, the data is grabbed from the tables and displayed. I can figure out the displaying part, just not how to send it to all the clients opening the method.

I tried using Net messages but they don’t seem to like me, Is there another way that i could do it?

Thanks =3

Best way? Use NET.

Serverside
The top of the script (Only run once)
[lua]
util.AddNetworkString( “Derp” )
[/lua]

To send the data:
[lua]
function SendData(ply,tab)
net.Start( “Derp” )
net.WriteTable(tab)
net.Send( ply )
end
[/lua]
(Yes net.WriteTable isn’t the best … but its the lazy way)

Clientside
[lua]
net.Receive( “Derp”, function( len, pl )
local tab = net.ReadTable()

end )

[/lua]

Can you clear up why this isn’t working please? :slight_smile:

When i run method one part of that method runs Code 1. Then code 2 is the secondary method which is meant to recieve the table from Code 1. However nothing seems to be running inside of the net.Receive method because no table is being printed in my client console!

Probably a really silly mistake (All of the Networked Strings and stuff are named and placed correctly)

Code 1:



		if SERVER then
			net.Start( "nameReports" )
				net.WriteTable(reportedName)
			net.Broadcast()
		end


Code 2:



		if CLIENT then
			net.Receive( "nameReports", function( len, pl )
				local tab = net.ReadTable()
				PrintTable(tab)
			end )			
		end
		


Put net.Receive outside of all functions or hooks. Also, you doesn’t seem to be calling util.AddNewtorkString.

I already called NetworkedString at the start of the script.

This worked but it printed the table in console once unfortunately the script is required to be running the net.broadcast quite frequently, Should it be running alot or is NET a once use function? ;/

edit:

Disregard that, i figured this out so far, however my next problem is i think my code is wrong.




	net.Receive( "nameReports", function( len, pl )
				if SERVER then
					net.Start("nameReportsClient")
					net.WriteTable(net.ReadTable())
					net.Broadcast()
				end
	end )
	
	net.Receive( "nameReportsClient", function( len, pl )
				if CLIENT then
					reportedName = net.ReadTable()
					PrintTable(net.ReadTable())
				end
	end )



So what i had to do was call method one which would call nameReports (server) which would call nameReportsClient (client) and this rather long and ugly process actually returned me with the thing i wanted, but i’m fairly sure this can be done with 1 net.Recieve rather than 2.

Anyone care to tell me? ;3

It obviously can be used multiple times, if it is only working once, it means you are not broadcasting anything from the server more than once.

^ Read my updated thread comment, I just figured that problem out with another problem haha :stuck_out_tongue:

Just don’t use the 2, use only one?


	
net.Receive( "nameReports", function( len, pl )
				if SERVER then
					net.Start("nameReportsClient")
					net.WriteTable(net.ReadTable())
					net.Broadcast()
				else
					reportedName = net.ReadTable()
					PrintTable(net.ReadTable())
				end
	end )


So you mean like this (Can’t test the code now)
I think i tried that piece of code and it didn’t work. Can’t remember if it was that specifically, but am i on the right track?

No, like this:



if ( SERVER ) then
    util.AddNetworkString( "Derp" )
    net.Start("nameReportsClient")
        net.WriteTable({"my","table","of","awesomness"})
    net.Broadcast()
else
    net.Receive( "nameReportsClient", function( len, pl )
            reportedName = net.ReadTable()
            PrintTable(reportedName )
    end )
end

Ok thanks i’ll try this code later on! :slight_smile: