User message (umsg) tutorial?

I’ve been to the GMod wiki but nothing really helps me understand how to use user messages to relay information to people–for example, I want to eventually use it to replace the datastream system currently in place inside DarkRP’s code.

Any help would be totally awesome. :slight_smile:

Say you wanted to tell a client when someone has died, and who killed them.

Clientside code would be:

usermessage.Hook(“PlayerKilledUM”, function( um ) – Make the hook
local ply = um:ReadString() – The dead player is the first string
local att = um:ReadString() – The killer is the second string
print( string.format( “Oh noes! %s was killed by %s!”, ply, att ) ) – Print to the console that att killed ply
end )


hook.Add(“DoPlayerDeath”, “PlayerKilledUM”, function( ply, att )
umsg.Start(“PlayerKilledUM”) – Start the usermessage. No player is specified so it’s sending it to all players
umsg.String( ply:Nick() ) – Send the first string as the dead player’s name
if ValidEntity( att ) and att:IsPlayer() then – Checking to see if the attacker is a player (you can’t call :Nick() on non-player entities)
umsg.String( att:Nick() ) – If it is, send the second string as their name
umsg.String(“Non-Player”) – Else, send the second string as “Non-Player”
end – End the check
end ) – End the function

umsg.End() at the end of that serverside part too, just after the “end the check” comment

FYI Datastream is just a lua module that uses usermessages to communicate with the client.

and console commands :cop:

For client to server communications. Not the other way around. :clint:

ya :bahgawd: