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

I didn't use copy and paste, but obviously i used most of the features here, after running it, i ran errors, then i switched to your code only as a test and saw that i ran errors. I guess i put a typo in there somewhere, because i mostly use %client.chatmessage

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;
}
This can be shortened by putting the returns before the messageClients, like so:
function serverCmdaddMoney(%client, %person, %amount)
{
   if(!%client.isAdmin)
      return messageClient(%client, '', "\c6This is an admin only command!");
   if(!strLen(%person))
      return messageClient(%client, '', "\c6Please enter a person's name to give them money.");
   if(!isObject(%otherclient = findClientbyName(%person)))
      return messageClient(%client, '', "\c6No such person exists!");
   if(!strLen(%amount))
      return messageClient(%client, '', "\c6Please enter a number.");
   else
   {
      %otherclient.setScore(%otherclient.getScore() += %amount);
      messageClient(%client, '', "\c6You have awarded \c3$" @ %amount SPC "\c6to" SPC %otherclient.getPlayerName() @ ".");
      messageClient(%otherclient, '', "\c6You have recieved \c3$" @ %amount SPC "\c6from" SPC %client.getPlayerName() @ ".");
   }
}

What's the point of the else {} block?

%otherclient.setScore(%otherclient.getScore() += %amount);


why are you using += please EDUCATE ME SIR



why are you using += please EDUCATE ME SIR
Are you not supposed to? Is it just +? There's no need to act like a richard about it, jeez. I made a simple mistake, you could just correct me and move on.
What's the point of the else {} block?
I know you're not supposed to do that because of the returns; it's just more of a personal preference.
« Last Edit: August 25, 2014, 08:32:29 PM by Cruxeis »


Are you not supposed to? Is it just +?

Don't worry about all of the pretentious kids in here. Just know, in the case above, you don't need to use +=, it'd just be +. Just a little logic error, don't worry yourself too much about it though.

Don't worry about all of the pretentious kids in here. Just know, in the case above, you don't need to use +=, it'd just be +. Just a little logic error, don't worry yourself too much about it though.
Thanks, elm. Are there specific cases where you should use += instead of + or vice versa?

Thanks, elm. Are there specific cases where you should use += instead of + or vice versa?
+= is only for when you want to add to an existing variable and modify its value. You cannot add a number to a function.