Er, it's not "onfunctionservercmd". Events are a bit different than coding. When coding, it's just "function serverCmd". Also, you can only define a function once. If you have the same function in there twice, the second one will overwrite the first one, also unlike events. So, that means you have to combine the two into one function. Blocki told you the best way to do it, but not why. If we converted your code into syntaxically correct code, it would still not work the way you wanted it to.
function serverCmdControl(%client, %player)
{
findClientByName(%player).setControlObject(%client);
findClientByName(%player).centerPrint("Remember to include a person to control!");
}
exec("./server.cs");
While this is close to working (it's only a bit off from having a player control another) it is not what you wanted. First off, this would be more of a "controlMe" command, because it would set your target in control of yourself. So first thing is you need to swap %client and findClientByName(%player). Same thing for the second line, you'd message the target saying to include a person to control, which would probably make no sense to them because they didn't type the command. The other big issue is that this is telling them to control their client, not their player. To get a player from a client, simply add ".player". So, once it's swapped, and player added:
function serverCmdControl(%client, %player)
{
%client.setControlObject(findClientByName(%player).player);
%client.centerPrint("Remember to include a person to control!");
}
exec("./server.cs");
This does more what you're looking for, it sets the person who types the command in control of the target. The biggest issue here is that it will always say "Remember to include a person to control!" regardless of if they included a person to control or not. To fix this, you have to add a check to make sure they typed in a person to control. A smaller issue is that this centerprint will be printed forever. To fix this, just add the number in seconds to the command as follows.
function serverCmdControl(%client, %player)
{
if(isObject(findClientByName(%player)))
{
%client.setControlObject(findClientByName(%player).player);
}
else
{
%client.centerPrint("Remember to include a player to control!", 3);
}
}
exec("./server.cs");
The "else" statement is equivilent to "otherwise". If the statement inside the if is false (if the target doesn't exist) it will tell them to remember to include a player to control. If the if statement is true, it will control their player. This is what you are looking for. However, what happens if the client is connected, but they are dead and their player doesn't exist? You'll want to make sure this isn't the case.
function serverCmdControl(%client, %player)
{
if(isObject(findClientByName(%player)))
{
if(isObject(findClientByName(%player).player))
{
%client.setControlObject(findClientByName(%player).player);
}
else
{
%client.centerPrint("That player is dead or hasn't spawned!", 3);
}
}
else
{
%client.centerPrint("Remember to include a player to control!", 3);
}
}
exec("./server.cs");
The last problem is the exec("./server.cs"); command. It will cause the file to execute itself, which will execute itself again, which will execute itself again, again forever. Just remove it. So then; the finished code is:
function serverCmdControl(%client, %player)
{
if(isObject(findClientByName(%player)))
{
if(isObject(findClientByName(%player).player))
{
%client.setControlObject(findClientByName(%player).player);
}
else
{
%client.centerPrint("That player is dead or hasn't spawned!", 3);
}
}
else
{
%client.centerPrint("Remember to include a player to control!", 3);
}
}
Blocki's looks much prettier, but is also a lot harder to understand for a beginner. This code will work just as well, and hopefully I explained it well enough that you understand it. Otherwise, post and one of us will clarify.