Jump to content

Recommended Posts

Posted (edited)

Hey, i am new to Ragemp coding and i am trying to get this code to work.

At the Moment i need to type    /freeze [Player Name] [true/false]

but I want that i can type           /freeze [ID] [true/false]

 

[Command("freeze", "/freeze a player ")]
        public void CMD_FreezePlayer(Player player,Player target, bool freezestatus)
        {
            Accounts account = player.GetData<Accounts>(Accounts.Account_Key);
            if (!account.IsPlayerAdmin((int)Accounts.AdminRanks.Supporter))
            {
                player.SendNotification("~r~You are not an Admin");
                return;
            }
            NAPI.ClientEvent.TriggerClientEvent(target, "PlayerFreeze", freezestatus);
            string freezeText = (freezestatus) ? "freezed" : "unfreezed";
            NAPI.Util.ConsoleOutput(player.Name + "  has  " + target.Name + " freezed/entfreezed " + freezestatus);
        }

Edited by Milkbottle
Posted
public static Player GetPlayerByNameOrId(string value)
        {
            // If value is number, we look in pool by ID
            if (int.TryParse(value, out int targetId))
            {
                return NAPI.Pools.GetAllPlayers().FirstOrDefault(X => X.Id == targetId);
            }
            // If value is not a number, we look by players name
            else
            {
                return NAPI.Pools.GetAllPlayers().FirstOrDefault(X => X.Name.Contains(value));
            }
        }

        [Command("freeze", "/freeze a player ")]
        public void CMD_FreezePlayer(Player player, string targetValue, bool freezestatus)
        {
            Accounts account = player.GetData<Accounts>(Accounts.Account_Key);
            if (!account.IsPlayerAdmin((int)Accounts.AdminRanks.Supporter))
            {
                player.SendNotification("~r~You are not an Admin");
                return;
            }
            Player target = GetPlayerByNameOrId(targetValue);
            if (target == null)
            {
                player.SendNotification("~r~Target not found");
                return;
            }
            NAPI.ClientEvent.TriggerClientEvent(target, "PlayerFreeze", freezestatus);
            string freezeText = (freezestatus) ? "freezed" : "unfreezed";
            NAPI.Util.ConsoleOutput(player.Name + "  has  " + target.Name + " freezed/entfreezed " + freezestatus);
        }

 

This should work, only thing I see here that may be problematic is you may get exception when using INT in argument where it expect string.

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...