[Tutorial] Creating cleaner and more read-able code
#11

Personally, I prefer to check everything before allowing the command to process, it saves all of that code being processed before realising 'oh wait, nvm I can't do that, get rekt kid mlg 360 noscopes and I just used data.

Simply, perform your checks at the top like this:

Code:
    if(AdminLevel[playerid] < 3) return SendClientMessage(playerid, COLOUR_GREY, "You are not authorized to use this command.");
    if(sscanf(params, "us[128]", giveplayerid, reason)) return SendClientMessage(playerid, COLOUR_GREY, "Usage: /kick [player id] [reason]");
	if(!IsPlayerConnected(giveplayerid)) return SendClientMessage(playerid, COLOUR_GREY, "That player is not connected.");
My Kick command looks tidier IMO.

Code:
CMD:kick(playerid, params[])
{
    if(LoggedIn[playerid] == 0) return SendClientMessage(playerid, COLOUR_GREY, "You must be logged in to use this command.");
    
    new reason[128], giveplayerid;
    if(AdminLevel[playerid] < 3) return SendClientMessage(playerid, COLOUR_GREY, "You are not authorized to use this command.");
    if(sscanf(params, "us[128]", giveplayerid, reason)) return SendClientMessage(playerid, COLOUR_GREY, "Usage: /kick [player id] [reason]");
    if(!IsPlayerConnected(giveplayerid)) return SendClientMessage(playerid, COLOUR_GREY, "That player is not connected.");
	
    format(reason, sizeof(reason), "Admin %s Kicked %s [Reason: %s ]", GetNameEx(playerid), GetNameEx(giveplayerid), reason);
    SendClientMessageToAll(COLOUR_REALRED, reason);
    SaveWeapons(giveplayerid);
    SavePlayerData(giveplayerid);
    SetTimerEx("KickPlayer",1000,false,"i",giveplayerid);// Kicks player in 500ms
    return 1;
}
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)