Hey guys, as I have been looking into it for a long time and found it by myslef, I decided to share since there's not much about UserMessages around.
I found a fancy way to put clients in a team on connect, I got to hook when the Team Menu VGUI appears when you join the server. It doesn't need a timer (might need to prevent crashes, see the "edit") and bypasses all the bugs related to it. The known bugs were when using "HookEvent("player_connect_full", ...);":
- Putting the player in a team using "ChangeClientTeam" would make it impossible to use the team menu, you could open the menu, but clicking a team would do nothing.
- Putting the player in a team using "CS_SwitchTeam" would make the game think you were not in that team, so when a entire team was eliminated, the game wouldn't give the win. And also would make the buy menu not usable.
You could "bypass" the bugs by adding a timer to put the client in a team when the "Continue" screen (
this one) was gone, but if the player minimize the game, the "Continue" screen is paused, making the timer useless, since it would trigger "ChangeClientTeam" with the "Continue" screen being showed.
Here is the solution:
PHP Code:
public void OnPluginStart()
{
HookUserMessage(GetUserMessageId("VGUIMenu"), TeamMenuHook, true);
}
public Action TeamMenuHook(UserMsg msg_id, Protobuf msg, const int[] players, int playersNum, bool reliable, bool init)
{
char buffermsg[64];
PbReadString(msg, "name", buffermsg, sizeof(buffermsg));
if (StrEqual(buffermsg, "team", true))
{
int client = players[0];
//Do stuff here
//You can acess the client's int as I defined it in "int client = players[0];"
//Edit: Be warned that if you change the client's team here, it might throw a fatal error and crash the server.
// To prevent it, use RequestFrame and pass the client index through it.
}
return Plugin_Continue;
}
Conclusion: only do ChangeClientTeam on player connect using this Hook, don't use "HookEvent("player_connect_full", ..., EventHookMode_Pre);" for this. You can use it for other stuff, tho. This way you will achieve the exact same way Valve does on their Match Making servers.