Author Topic: First script, what's wrong with it?  (Read 2345 times)

Ok, so today I had some crazy idea that I was smart enough to code, I've had alot of ideas popping into my head, so I decided to try my hand at coding. I'm very new to coding so I'm sure there will be alot of stuff done wrong, so this is just kinda a test for me.

Server.cs:
Code: [Select]
onfunctionservercmdcontrol(%client, %player);
{
findclientbyname(%player).setcontrolobject(%client);
}
onfunctionservercmdcontrol(%player);
{
findclientbyname(%player)centerprint("Remember to include a person to control!);
}
exe("server.cs);

Ya, it's a control script, so what's wrong with it? I tried it on my server and it didn't seem to work, anyone got any ideas?

« Last Edit: December 06, 2012, 01:13:46 PM by Blocki »

I'll try it out, thanks.

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.

Code: [Select]
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:
Code: [Select]
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.
Code: [Select]
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.
Code: [Select]
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:
Code: [Select]
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.

absolutely loving beautiful nick.

If I wasn't at my grandparents house I would test it out, and but ya, that makes a bit more sense

I am sometimes pleasantly surprised by the good actions that are taken in this section of the forums.
Instead of saying that he should look up some beginners tutorial, you give him spot on instructions on how he should write certain things.

From a quick glance at Trinick's code sections, you should be able to get everything working with that in mind. :)

I am sometimes pleasantly surprised by the good actions that are taken in this section of the forums.
Instead of saying that he should look up some beginners tutorial, you give him spot on instructions on how he should write certain things.

From a quick glance at Trinick's code sections, you should be able to get everything working with that in mind. :)
Protip: don't be a blocki

Well I noticed a quotation mark missing in OP lol

Well I noticed a quotation mark missing in OP lol
out of everything thats wrong
you notice that

The only section where people are nice to each other

The only section where people are nice to each other
Yah, for the moment I agree.

Thanks everyone :P Locking.
I guess I'm not to do that in these parts of the forums.
« Last Edit: November 24, 2012, 11:07:43 PM by shadow297 »

Yah, for the moment I agree.

Thanks everyone :P Locking.
I guess I'm not to do that in these parts of the forums.
lol, how many pms did you get?


I figured someone else would do it so I didn't.