Jump to content

C# Command with PlayerID


Recommended Posts

For example if I want to give weapon to someone with command I need to type:

/gweapon [PlayerName] [weapon] [ammo]

but I dont want to use playerName, instead I want to use playerID.

[Command("gweapon", "~b~/gweapon ~w~[Ime/id] [Weapon Name] [Ammo]")]
        public void CMD_Gweapon(Player player, Player target, string weapon, int ammo)
            if (!IPlayer.IsPlayerLoggenIn(player)) return;

            IPlayer iplayer = player.GetData<IPlayer>("PlayerData");
            if (iplayer.HasAdminLevel((int)AdminRanks.Administrator))
                WeaponHash weaponHash = NAPI.Util.WeaponNameToModel(weapon);
                target.GiveWeapon(weaponHash, ammo);

                target.SendChatMessage("~g~[Success]~w~ Oruzije: ~g~" + weapon + " ~w~uspesno setovano!");

                Logs.LogPlayerData(player, player.Name + " je setovao oruzije igracu: " + target.Name + ". Oruzije: " + weapon + ". Municija: " + ammo + ". | Vreme: " + DateTime.Now);
                Logs.LogPlayerData(target, "Administrator: " + player.Name + " vam je setovao oruzije: " + weapon + ". Municija: " + ammo + ". | Vreme: " + DateTime.Now);
            else NonAdminMessage(player);

How to get Player from id?

Link to comment
Share on other sites


To get a player by the ID, you can check the Players Pool and get the player who's .Value is equal to the desired id, like so:

NAPI.Pools.GetAllPlayers().FirstOrDefault(x => x.Value == target);

And in your command, instead of Player target, change it to int target and use that id.

Or, if you're interested in a more robust and dynamic version, you can use string type instead of int (for target) and that way, you could use a command with Name or Id, whichever is desired. But you'd have to try and parse that string into an int and if successful, use that id in the above code. Otherwise (if by name), you'd use

NAPI.Player.GetPlayerFromName(targetString); // where targetString is the command's argument

ps: GetPlayerFromName only works with full names. So you can't use "Vende", you'd have to use "Vendeta" if a player's name is Vendeta.

Edited by Tonytza
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...