Can't get a part of a code to work

Author Topic: Can't get a part of a code to work  (Read 1733 times)

It runs no errors, but features like, find client by name and invalid name are not functioning whatsoever, i changed something, everything stopped working and now i can't figure it out anymore
Code: [Select]
function serverCmdGrantmoney(%client,%money,%name)
{
if(%client.isSuperAdmin)
{
if(%name !$="")
{
if(isObject(%target = findClientByName(%name)))
{
if(%target != %client)
{
messageClient(%client, '', "<font:impact:20><color:ffffff>You grant <color:006600>$" @ %money SPC "\<font:impact:20><color:ffffff>to " @ %target.name @ ".");
messageClient(%target, '', "<color:ffffff>An admin has granted you <color:006600>$" @ %money @ "<color:ffffff><font:impact:20>.");
}
else
messageClient(%client, '', "\c6You grant yourself \c3$" @ %money @ "\c6.");
%target.score+=%money;
}
else
%client.chatMessage("Player not found");
}
else
%client.chatMessage("<color:ffffff><font:impact:20>You grant yourself <color:006600>$" @ %money);
%client.score+=money;
else if(isObject(%client.player))

if(isObject(%target))
{
messageclient(%client, '', "<color:ffffff><font:impact:20>You grant <color:006600>$" @ %money SPC "<color:ffffff><font:impact:20>to" SPC %target.name @ "<color:ffffff><font:impact:20>.");
messageClient(%target, '', "<color:ffffff>An admin has granted you <color:006600>$" @ %money @ "<color:ffffff><font:impact:20>.");

%target.score+=%money;
}
}
}
else
messageClient(%client, '', "<color:ffffff><font:impact:20>You must be super admin to use the this command.");
}

You can't do a bracketless else statement to encapsulate more than one statement. It'll only cover the next statement. You should probably add the brackets to all your elses, I bet that's causing some errors. Also for line 7, it should be %target ==.

Try formatting your code like this as a test, it should work like this and I think it will help you understand.
Code: [Select]
function serverCmdgrantMoney(%client, %name, %money)
{
if(!%client.isSuperAdmin)
{
messageClient(%client,'',"<color:ffffff><font:impact:20>You must be super admin to use the this command.");
return;
}
%target = findClientByName(%name);
if(%name $= "")
{
%client.chatMessage("no target");
return;
}
if(%money $= "")
{
%client.chatMessage("no money");
return;
}
if(!isObject(%target.player))
{
%client.chatMessage("Client does not have a player object.");
return;
}
if(%target = %client)
{
messageClient(%client, '', "\c6You grant yourself \c3$" @ %money @ "\c6.");
%target.score +=%money;
return;
}
else
{
messageClient(%client, '', "<font:impact:20><color:ffffff>You grant <color:006600>$" @ %money SPC "\<font:impact:20><color:ffffff>to " @ %target.name @ ".");
messageClient(%target, '', "<color:ffffff>An admin has granted you <color:006600>$" @ %money @ "<color:ffffff><font:impact:20>.");
return;
}
}
« Last Edit: August 25, 2014, 06:13:14 PM by Crøwn »

Also for line 7, it should be %target ==.
Wrong. He's defining %target while checking if it exists. Dannu is right on that.

Wrong. He's defining %target while checking if it exists. Dannu is right on that.
Ah. So do assignment statements return the assigned value? (in this case findClientByName(%name))

Ah. So do assignment statements return the assigned value? (in this case findClientByName(%name))
Yes.

You can't do a bracketless else statement to encapsulate more than one statement. It'll only cover the next statement. You should probably add the brackets to all your elses, I bet that's causing some errors.
Actually, the brackets are fine, its his indentation which is wrong. No matter whether %target is the same as %client, the score should be increased.

Edit: Actually, there are cases where the else statements should be bracketed. I only saw the case on line 16.
« Last Edit: August 25, 2014, 01:25:21 PM by boodals 2 »

So wait, Torquescript takes indentation into account??? I always thought bracketless conditionals only applied to the next statement

So wait, Torquescript takes indentation into account???
No
I always thought bracketless conditionals only applied to the next statement
Yes

So any ideas on what i might be doing wrong?

Try restructuring it like crown posted

Code: [Select]
if(!condition)
    return;

//code here

is much easier to follow than

Code: [Select]
if(condition)
{
    //code here
}

when you have a bunch of different conditions you all need to check and they're all nested in each other

I think simply putting a return; after %client.chatMessage("Player not found"); , deleting the next else, then deleting everything indented from and including else if(isObject(%client.player)) (lines 24 to 33) should fix it, although it can be done a lot neater and more efficiently.

Someone explain return because there's no sign that he understands it. I would but I can't word it nicely.

Also, wow, I've never seen a Coding Help topic so busy..
« Last Edit: August 25, 2014, 01:33:41 PM by boodals 2 »

So any ideas on what i might be doing wrong?
You can't do a bracketless else statement to encapsulate more than one statement. It'll only cover the next statement. You should probably add the brackets to all your elses, I bet that's causing some errors.
As in

Code: [Select]
else
messageClient(%client, '', "\c6You grant yourself \c3$" @ %money @ "\c6.");
%target.score+=%money;
Code: [Select]
else
%client.chatMessage("<color:ffffff><font:impact:20>You grant yourself <color:006600>$" @ %money);
%client.score+=money;
Code: [Select]
else if(isObject(%client.player))
if(isObject(%target))

Those three else's only target the next statement. Also on %client.score+=%money; you forgot the %

Crown, your code runs quite a few errors that i can't figure out for the life of me :/

This is the code I use to give money

Code: [Select]
function serverCmdgiveMoney(%client, %input, %amt)
{
if(!%client.isAdmin)
{
messageClient(%client, '', "\c4This is an admin only command.");
return;
}
%giftee = findClientByName(%input);
if(!isObject(%giftee))
{
%giftee = findClientByBL_ID(%input);
}
if(!isObject(%giftee))
{
messageClient(%client, '', "\c3The selected client could not be found.");
return;
}
if(%amt < 0)
{
messageClient(%client, '', "\c3You can't give negative Money .");
return;
}
if(%amt > 5000)
{
messageClient(%client, '', "\c3Don't give more than 5000 Money at a time.");
return;
}
messageClient(%giftee,'',"\c3You have been given\c2$" SPC %amt SPC "\c3 by\c2" SPC %client.getPlayerName() @ "\c3.");
messageClient(%client,'',"\c3You have given\c2$" SPC %amt SPC "\c3 to\c2" SPC %giftee.getPlayerName() @ "\c3.");
%giftee.score += %amt;
}

Crown, your code runs quite a few errors that i can't figure out for the life of me :/
It wasn't mean to be a "copy and paste use this" but I accidentally put an extra ' in the messageClient line, sorry man. Other than that though it works exactly as you wanted yours to work.

I edited my post, it should work fine now.