Thread: [Solved] Save user for x time
View Single Post
azalty
AlliedModders Donor
Join Date: Feb 2020
Location: France
Old 06-11-2022 , 18:28   Re: Save user for x time
Reply With Quote #4

Quote:
Originally Posted by eyal282 View Post
Use a database or clientprefs to save across server restarts.
Using a database for that isn't really the best solution. The problem here is that people can just reconnect on the same map and respawn.

Thus, we don't need that info to persist after a server restart, or even, a map change.

Code:
ArrayList g_hConnectHistoryList;
char g_sBuffer[32];
bool g_bShouldRespawn[MAXPLAYERS + 1];

public void OnPluginStart()
{
	g_hConnectHistoryList = new ArrayList(ByteCountToCells(32)); // Let's use a size of 32 for SteamIDs
}

void RespawnPlayer(int client)
{
	// Write your code here to respawn the player, ex:
	CS_RespawnPlayer(client);
}

public void OnMapStart()
{
	g_hConnectHistoryList.Clear();
}

public void OnClientDisconnect(int client)
{
	g_bShouldRespawn[client] = false;
}

public void OnClientAuthorized(int client, const char[] auth)
{
	if (g_bChecked[client])
		return;
	
	if (g_hConnectHistoryList.FindString(auth) == -1)
	{
		if (!IsClientInGame(client))
		{
			g_bShouldRespawn[client] = true;
			return;
		}
		g_hConnectHistoryList.PushString(auth);
		
		RespawnPlayer(client);
	}
}

public void OnClientPutInServer(int client)
{
	if (!g_bShouldRespawn[client])
		return;
	GetClientAuthId(client, AuthId_Steam2, g_sBuffer, sizeof(g_sBuffer));
	g_hConnectHistoryList.PushString(g_sBuffer);
	
	RespawnPlayer(client);
}
If you just want to limit this per round, just hook the "round_prestart" event (because when the start event is called, all the players have already spawned) and clear the arraylist when that event is called.
__________________
GitHub | Discord: @azalty | Steam

Last edited by azalty; 06-11-2022 at 18:35. Reason: bug fix
azalty is offline