I am not sure if I am using usermessages correctly

Hello, I am making a HUD element and I want it to display (for CP’s only) who is currently wanted.

I have this code, which makes the HUD element: (clientside of course)

[LUA]

local defaultwantedplayers = {
“No players wanted.”
}

local wantedplayers = {

}

function CheckWantedPlayers()

if wantedplayers == nil then
wantedplayers = table.Copy( defaultwantedplayers )
end

end

function DrawCopHUD()
if LocalPlayer():isCP() then
local X = ScrW()/2-750
local Y = 0
local W = 500
local H = 290

surface.SetDrawColor( 0, 0, 0, 200 )
surface.DrawRect( X, Y, W, 35+(#defaultwantedplayers*20) )

draw.RoundedBox( 4, X, Y, W, 30, Color( 0, 0, 100, 200 ) )
draw.DrawText("WANTED PLAYERS", "TargetID", X+W/2, Y+7, Color( 255, 0, 0, 200 ), TEXT_ALIGN_CENTER )

local y = 35
for i, wplayer in pairs( wantedplayers ) do

	draw.DrawText( i .. ": " .. wplayer, "TargetID", X+5, y, Color( 255, 255, 255, 200 ) )
	
	y = y + 20
	
end
end

end
hook.Add(“HUDPaint”, “DrawCopHUD”, DrawCopHUD)

function AddWantedPlayer( um )

	table.insert( wantedplayers, um:ReadString() )

end
usermessage.Hook(“AddWantedPlayer”, AddWantedPlayer )
[/LUA]

And this is my serverside that checks if a player is wanted:

[LUA]

local defaultwantedplayers = {
“No players wanted.”
}

local wantedplayers = {

}

function CheckWantedPlayers()

if wantedplayers == nil then
wantedplayers = table.Copy( defaultwantedplayers )
end

end

function DrawCopHUD()
if LocalPlayer():isCP() then
local X = ScrW()/2-750
local Y = 0
local W = 500
local H = 290

surface.SetDrawColor( 0, 0, 0, 200 )
surface.DrawRect( X, Y, W, 35+(#defaultwantedplayers*20) )

draw.RoundedBox( 4, X, Y, W, 30, Color( 0, 0, 100, 200 ) )
draw.DrawText("WANTED PLAYERS", "TargetID", X+W/2, Y+7, Color( 255, 0, 0, 200 ), TEXT_ALIGN_CENTER )

local y = 35
for i, wplayer in pairs( wantedplayers ) do

	draw.DrawText( i .. ": " .. wplayer, "TargetID", X+5, y, Color( 255, 255, 255, 200 ) )
	
	y = y + 20
	
end
end

end
hook.Add(“HUDPaint”, “DrawCopHUD”, DrawCopHUD)

function AddWantedPlayer( um )

	table.insert( wantedplayers, um:ReadString() )

end
usermessage.Hook(“AddWantedPlayer”, AddWantedPlayer )
[/LUA]

There are no console errors outputted, I must just be using something wrong?
Any help appreciated, thanks.

I don’t mean to be that guy that goes ‘wow why are you using X, use Y’ instead of answering the question, but I suppose I am.

Why not use net messages? I’ll just assume you don’t know about them/how to use them.

To use a net message, you first need to run this on the server:

[lua]util.AddNetworkString(‘my_message_name’)[/lua]
This must be done for any message name you wish to use, including those sent from a client to the server.

To send a net message, you start it, write the data, and send it. The data on the receiving end must read it in the same order (string, number, table, for instance).

You can send and receive messages from both the client and the server.
Receiving looks like this:

[lua]net.Receive(‘my_message_name’,function(length,ply) //ply is only used if the server is retrieving the message, representing the sending player; length is the bytes of data that were sent
local something = net.ReadTable() //or read something else, just make sure you use net.Read* in the same order you used net.Write*
//the local variable was created so that you can quickly get all of the data read and reference it in your code multiple times (after data is read, it is taken out of the data pool and can’t be referenced again via net.Read*)
//do stuff
end)[/lua]
You can find a list of Read and Write functions here. You might find it easier (improper, although it won’t really matter) to send everything in one big table such as ‘net.WriteTable({data,data,data})’, because tables can hold all of the other data types.

To send a message, you start with net.Start(), but the finish varies depending on what you want to do. If you are a client sending information to the server, you use net.SendToServer(). The client can only send information to the server, not to other clients. As a server, you can send the message to ALL players with net.Broadcast(), or to specified players with net.Send(player object or table containing players can go here). Examples of net.Send are net.Send(thatplayer), net.Send({thatplayer,bob}).

[lua]net.Start(‘my_message_name’)
net.WriteTable({table contents here})
net.Send(player)[/lua]

The net library is great because it makes client<->server communication a breeze. If I remember correctly, usermessages can not send information from the client to the server, forcing you to use console commands (but the net library can).

[lua]net.Receive(‘AddWantedPlayer’,function(length)
table.insert(wantedplayers,net.ReadString())
end)[/lua]

As to your actual problem, I have no idea because you accidentally pasted your clientside code in both blocks, instead of showing us your serverside code. So I can’t tell you what to replace the server code with, but my explanation of net usage should make it clear enough.

Oh, I didn’t even notice, how idiotic of me :pwn:

[LUA]

AddCSLuaFile(“cl_init.lua”)

local defaultwantedplayers = {
“No players wanted”
}

function CheckWantedPlayers()

if wantedplayers == nil then
local wantedplayers = table.Copy( defaultwantedplayers )
end

end

function AddWantedPlayerToTable( criminal )
for k, v in pairs(player.GetAll()) do
if v:isWanted() == true then
umsg.Start(“AddWantedPlayer”)

			umsg.String( criminal )
		
		umsg.End()
	end
end

end
hook.Add(“playerWanted”, “AddWantedPlayersToHUD”, AddWantedPlayerToTable)
[/LUA]

For a start, the playerWanted hook (assuming that capitalization is correct) would surely be using a player as the argument instead of the player’s name? So umsg.String(criminal) might need replacing with umsg.String(criminal:Nick()), but I could be wrong, I don’t know in what way that RP gamemode is written.

Server:
[lua]
util.AddNetworkString(‘AddWantedPlayer’)
local function AddWantedPlayerToTable(criminal)
local t = {}
net.Start(‘AddWantedPlayer’)
for k, v in pairs(player.GetAll()) do
if v:isWanted() then
table.insert(t,v:Nick()) //or perhaps just v? depends what this criminal argument is
end
end
net.WriteTable(t)
net.Broadcast()
end
[/lua]

Client:
[lua]
net.Receive(‘AddWantedPlayer’,function(length)
wantedplayers = net.ReadTable() //you’re rebuilding the whole wanted list each time someone is wanted, so you don’t want to keep adding users that were wanted the entire time; just set wantedplayers to the received table since now you are sending it all at once instead of once per wanted player
end)
[/lua]