The online racing simulator
Command ban
(18 posts, started )
Command ban
Hello guys !

I created a new command but not working and i really don't know what's the problem
Here is the code:

[Command("ban", "ban <username> <days> <reason>")]
public void ban(string Msg, string[] StrMsg, Packets.IS_MSO MSO)
{
clsConnection Conn = Connections[GetConnIdx(MSO.UCID)];

if (StrMsg.Length > 2)
{
if (Conn.IsAdmin == 1 || Conn.IsSuperAdmin == 1 || Conn.IsModerator == 1)
{
bool BanUserfound = false;
int Days;
foreach (clsConnection C in Connections)
{
string Message20 = Msg.Remove(0, C.Username.Length + 6);

if (C.Username == StrMsg[1] && StrMsg[1].Length > 1)
{
BanUserfound = true;

Message("/ban " + C.Username + Days);
MsgAll("^6» " + C.PlayerName + " ^6was banned by " + Conn.PlayerName);
MsgAll("^6» Reason: ^7" + Message20);

}
}
if (BanUserfound == false)
{
InSim.Send_MTC_MessageToConnection("^7» Wrong username.", MSO.UCID, 0);
}
}

}
else
{
InSim.Send_MTC_MessageToConnection("^7» Wrong command", MSO.UCID, 0);
}

}

So what is the error or what happens?
Is an error " Use of unassigned local variable 'Days' "
It's a very simple problem. You have made variable Days but it's not assigned. Exactly what the error tells you. You first have to set the Days variable.
And sow to set this variable ?
I'm coding in C#
Sorry i'm sleepy... i setted variable int Days = 0; ... but if i ban someone for 2/5/8 (is an example) in message says "You are banned for 12 hours"
Here is the new code :

[Command("ban", "ban <username> <days> <reason>")]
public void ban(string Msg, string[] StrMsg, Packets.IS_MSO MSO)
{
clsConnection Conn = Connections[GetConnIdx(MSO.UCID)];

if (StrMsg.Length > 2)
{
if (Conn.IsAdmin == 1 || Conn.IsSuperAdmin == 1 || Conn.IsModerator == 1)
{
bool BanUserfound = false;
int Days = 0;
foreach (clsConnection C in Connections)
{
string Message20 = Msg.Remove(0, C.Username.Length + 6);

if (C.Username == StrMsg[1] && StrMsg[1].Length > 1)
{
BanUserfound = true;

Message("/ban " + C.Username + Days);
MsgAll("^6» " + C.PlayerName + " ^6was banned by " + Conn.PlayerName);
MsgAll("^6» Reason: ^7" + Message20);

if (Days == 0)
{
MsgPly("^6» You are banned for 12 hours", C.UniqueID);
}
else if (Days == 1)
{
MsgPly("^6» You are banned for " + Days + " Day", C.UniqueID);
}
else
{
MsgPly("^6» You are banned for " + Days + " Days", C.UniqueID);
}

BanID(C.Username, Days);

}
}
if (BanUserfound == false)
{
InSim.Send_MTC_MessageToConnection("^7» Wrong username.", MSO.UCID, 0);
}
}

}
else
{
InSim.Send_MTC_MessageToConnection("^7» Wrong command", MSO.UCID, 0);
}

}

Anyone please :|
Quote :i setted variable int Days = 0; ... but if i ban someone for 2/5/8 (is an example) in message says "You are banned for 12 hours"

You set Days=0 and then never change its value.
So of course it stays at 0.

int Days = 0;
.
.
.
if (Days == 0)
{
MsgPly("^6» You are banned for 12 hours", C.UniqueID);

And how can I make it for multiple days?
int Days = int.Parse(BTT.Text); .....
try this
and u must change the BTT.Text to what u have .. stringMsg
Quote from play.net :int Days = int.Parse(BTT.Text); .....
try this
and u must change the BTT.Text to what u have .. stringMsg

Not working...
Please guys help me with this value
Of course it doesn't work, nowhere you are busy with buttons.

If StrMsg[1] is the username then I presume StrMsg[2] is the day specification. So; Days = StrMsg[2] .. This is very basic logical coding to be honest.
Quote from cargame.nl :Of course it doesn't work, nowhere you are busy with buttons.

If StrMsg[1] is the username then I presume StrMsg[2] is the day specification. So; Days = StrMsg[2] .. This is very basic logical coding to be honest.

I tried that but not working :|
Exactly. Most people here copy a random cruise InSim and go around copy random functions from other people without any basic knowledge in programming at all and then come here saying it doesn't work.

Maybe you guys should just actually write things yourself. Start simple and work your way up. Don't expect to master this in a week, but you have to give it some time and do some research. We're not here for building you an InSim.

Oh, and please, for the love of god, set some breakpoints at a few lines, and start debugging. Look what values you receive so you can easily see what you have to do to access certain things.

You indeed have to look at the direction cargame.nl pointed you at. What you receive is "!ban usernamehere 10 reasonhere". With the split option you can split a string into parts that are put into an array of strings. If you split on a space you would get an array with the words separated:

array[0] = usernamehere
array[1] = 10
array[2] = reasonhere

This might not be the best option (since usernames and/or reasons might contain spaces), but it's to give you a simple example.

Command ban
(18 posts, started )
FGED GREDG RDFGDR GSFDG