Help with script (C#)

Hello,
I saw alot of people having colored names. I dont want that on my server so i created this script:

using CitizenFX.Core;
using static CitizenFX.Core.Native.API;

namespace NoFormatName
{
    public class Class1
    {
        private void HasCon()
        {
            rescan:;
            Wait(300000);
            PlayerList players = new PlayerList();
            foreach (Player player in players)
            {
                if (player.Name.Contains("^1") || player.Name.Contains("^~") || player.Name.Contains("^2") || player.Name.Contains("^3") || player.Name.Contains("^4") || player.Name.Contains("^5") || player.Name.Contains("^6") || player.Name.Contains("^6") || player.Name.Contains("^7") || player.Name.Contains("^8") || player.Name.Contains("^9") || player.Name.Contains("^*") || player.Name.Contains("^_") || player.Name.Contains("^=") || player.Name.Contains("^*^"))
                {
                    int pID = player.ServerId;

                    ExecuteCommand("clientkick " + pID + " Dein Name darf keine Formatierungs-Codes enthalten!");
                    ExecuteCommand("say Der Spieler (ID: " + pID + ") wurde gekickt!");
                    goto rescan;
                }
                goto rescan;
            }
        }
    }
}

It should check every 5 minutes, if a player as a formating-code in his name and kick him.
This doesnt work, i didn’t found a mistake. Can someone help?

My __resource.lua:

resource_manifest_version '05cfa83c-a124-4cfa-a768-c24a5811d8f9'

name 'NoFormatName'
description 'Kicks players having formating-codes in their names.'
author 'Agiger03'
version '1.0'

files {

}

client_scripts {

}

server_scripts {
    'NoFormatName.dll',
}

I think u don’t need to end your label with a ;
If I read this correctly: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/goto

Are there any errors in your F8?

You are a german boy or?

@Naifen yes i am.

@onno204 nope, did not help.

In wich Server are you developing

@Naifen my own (Giger-Netzwerk: https://agiger03.ga)
but i would like to work for another server.

This code will not repeat itself. Heck, it will actually never be called. What you need is to run this code in a Tick. Also, don’t use Wait() like this in C#.

Lets rewrite it:

namespace NoFormatName
{
    public class Class1
    {
        public Class1()
        {
            Tick += OnTick;
        }

        private async Task OnTick()
        {
            await Delay(300000);
            PlayerList players = new PlayerList();
            foreach (Player player in players)
            {
                if (player.Name.Contains("^1") || player.Name.Contains("^~") || player.Name.Contains("^2") || player.Name.Contains("^3") || player.Name.Contains("^4") || player.Name.Contains("^5") || player.Name.Contains("^6") || player.Name.Contains("^6") || player.Name.Contains("^7") || player.Name.Contains("^8") || player.Name.Contains("^9") || player.Name.Contains("^*") || player.Name.Contains("^_") || player.Name.Contains("^=") || player.Name.Contains("^*^"))
                {
                    int pID = player.ServerId;

                    Debug.WriteLine("clientkick " + pID + " Dein Name darf keine Formatierungs-Codes enthalten!");
                    Debug.WriteLine("say Der Spieler (ID: " + pID + ") wurde gekickt!");
                }
            }

        }
    }
}

To actually say something in chat, have a look at the chat resource for appropriate events (TriggerClientEvent()). For kicking the person you can use DropPlayer.

Also: You MUST change your assembly name to NoFormatName.net and target .NET Framework 4.5.2

Please read http://docs.fivem.net/scripting-manual/runtimes/csharp/

@d0p3t Thanks for your reply.
“Tick” and “Delay” get underlined red.

Yes the class needs to extend the BaseScript class.

public class Class1
{
    ...
}

to

public class Class1 : BaseScript
{
    ...
}

This requires to add a reference to the CitizenFX libraries. And also the culprit as to why your code is not executing.

@Syntasu so it should look like this?:

using CitizenFX.Core;

namespace NoFormatName
{
    public class Class1 : BaseScript
    {
        private void that()
        {
            rescan:
            Delay(300000);
            PlayerList players = new PlayerList();
            foreach (Player player in players)
            {
                if (player.Name.Contains("^1") || player.Name.Contains("^~") || player.Name.Contains("^2") || player.Name.Contains("^3") || player.Name.Contains("^4") || player.Name.Contains("^5") || player.Name.Contains("^6") || player.Name.Contains("^6") || player.Name.Contains("^7") || player.Name.Contains("^8") || player.Name.Contains("^9") || player.Name.Contains("^*") || player.Name.Contains("^_") || player.Name.Contains("^=") || player.Name.Contains("^*^"))
                {
                    int pID = player.ServerId;

                    Debug.WriteLine("clientkick " + pID + " Dein Name darf keine Formatierungs-Codes enthalten!");
                    Debug.WriteLine("say Der Spieler (ID: " + pID + ") wurde gekickt!");
                    goto rescan;
                }
                goto rescan;
            }
        }
    }
}

Use the code that @d0p3t send, but than with the BaseScript.

You should read the guide on the docs to iron out the quirks:

That guide isn’t finished yet though, and no actual code has been shown on there yet.

@Syntasu i wanted to use his code, but then “Tick” is underlined red.

Yeah but setting up the project is important to, he has got the code aspect of it, dunno if he did stuff like adding the .net part to the assembly name and such.

Yes, because Tick and Delay are defined in the BaseScript, inheriting from BaseScript should remove these errors/underlines.

@Syntasu there are now no errors in my code.
But when i start the server:

Exception thrown by a task: System.AggregateException: One or more errors occurred. ---> System.MissingMethodException: int CitizenFX.Core.Player.get_ServerId()
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] in <49fde7938a9b47b480042d844e818c7e>:0
  at NoFormatName.Class1.OnTick () [0x0001b] in <c8716da1c0c14b8396dafd3babfb2ac8>:0
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <49fde7938a9b47b480042d844e818c7e>:0
  at System.Threading.Tasks.Task.Execute () [0x00010] in <49fde7938a9b47b480042d844e818c7e>:0
   --- End of inner exception stack trace ---
---> (Inner Exception #0) System.MissingMethodException: int CitizenFX.Core.Player.get_ServerId()
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] in <49fde7938a9b47b480042d844e818c7e>:0
  at NoFormatName.Class1.OnTick () [0x0001b] in <c8716da1c0c14b8396dafd3babfb2ac8>:0
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <49fde7938a9b47b480042d844e818c7e>:0
  at System.Threading.Tasks.Task.Execute () [0x00010] in <49fde7938a9b47b480042d844e818c7e>:0 <---

Failed to run a tick for Class1: System.MissingMethodException: int CitizenFX.Core.Player.get_ServerId()
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] in <49fde7938a9b47b480042d844e818c7e>:0
  at NoFormatName.Class1.OnTick () [0x0001b] in <c8716da1c0c14b8396dafd3babfb2ac8>:0
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <49fde7938a9b47b480042d844e818c7e>:0
  at System.Threading.Tasks.Task.Execute () [0x00010] in <49fde7938a9b47b480042d844e818c7e>:0

player.ServerId does not exist on the server, I guess try

int pID = player.Handle;

Not sure if this is the correct id though.

@Syntasu nahh…

Exception thrown by a task: System.AggregateException: One or more errors occurred. ---> System.MissingMethodException: int CitizenFX.Core.Player.get_Handle()
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] in <49fde7938a9b47b480042d844e818c7e>:0
  at NoFormatName.Class1.OnTick () [0x0001b] in <7d80e2549de64f82a7f342c20c0fa808>:0
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <49fde7938a9b47b480042d844e818c7e>:0
  at System.Threading.Tasks.Task.Execute () [0x00010] in <49fde7938a9b47b480042d844e818c7e>:0
   --- End of inner exception stack trace ---
---> (Inner Exception #0) System.MissingMethodException: int CitizenFX.Core.Player.get_Handle()
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] in <49fde7938a9b47b480042d844e818c7e>:0
  at NoFormatName.Class1.OnTick () [0x0001b] in <7d80e2549de64f82a7f342c20c0fa808>:0
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <49fde7938a9b47b480042d844e818c7e>:0
  at System.Threading.Tasks.Task.Execute () [0x00010] in <49fde7938a9b47b480042d844e818c7e>:0 <---

Failed to run a tick for Class1: System.MissingMethodException: int CitizenFX.Core.Player.get_Handle()
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] in <49fde7938a9b47b480042d844e818c7e>:0
  at NoFormatName.Class1.OnTick () [0x0001b] in <7d80e2549de64f82a7f342c20c0fa808>:0
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <49fde7938a9b47b480042d844e818c7e>:0
  at System.Threading.Tasks.Task.Execute () [0x00010] in <49fde7938a9b47b480042d844e818c7e>:0

set debugging to embedded in the project settings, it’ll give a better error in the console.

@Vespura ? sorry, i dont get that.