Author Topic: If statement isn't working  (Read 662 times)

Code: [Select]
//#######################################
//#
//# Made by Brighten
//# Version 0.0.1
//#
//#######################################

$version = "0.0.1";

$KB_AB_active = true;
newChatHud_AddLine("\c4KipBot Auto Buy Version:" SPC $version SPC "activated");
$buy = false;

package KB_UM_Pack
{

function clientCmdBottomPrint(%text, %time, %a)
{
echo("Bottom Print");
echo("Text:" SPC %text);
Parent::clientCmdBottomPrint(%text, %time, %a);
if(!$kb_ab_active)
return;
%nextPick = getWord(%text, 16);
%nextPick = stripchars(%nextPick, "$,");
%currentMoney = stripchars(getWord(%text, 2), "$,");

echo("Next Pick:" SPC %nextPick);
echo("Current Money:" SPC %currentMoney);
$buy = true;
                // VVVVVVVVVVVVVVVVVVVVVVVv
if(%currentMoney < %nextPick) // This if is definitely the problem
{
$buy = false;
}
if($buy $= true)
{
BuyPick();
}


}

};
activatePackage(KB_UM_Pack);

function BuyPick()
{
commandToServer('upgradePickS');
newChatHud_AddLine("\c4[KipBot] \c2auto bought a pick level");
}

function toggleKBAB()
{
if($kb_ab_active)
{
$kb_ab_active = false;
newChatHud_AddLine("\c4KipBot Auto Buy is \c0DISABLED");
}else if(!$kb_ab_active)
{
$kb_ab_active = true;
newChatHud_AddLine("\c4KipBot Auto Buy is \c2ENABLED");
}
}

output
>Next Pick: 1582
>Current Money; 1360;

for some reason it isn't checking it properly.
I don't know whats wrong, please help

please do not check true/false values using $=
the $= operator is a string equals
torquescript treats true or false as 1 or 0

Use one one of the following methods for checking if a variable is true/false
Code: [Select]
if(%var)
{

}
if(%var == 1)
{

}
if(%var == true)
{

}
the first method if(%var) is the equivalent of
Code: [Select]
if(%var != 0)
{

}


also inside of your function scope you should be using a local variable instead of a global variable
local variables are delineated with a % instead of a $
there is no reason to make buy a global variable in this case


my bet is the issue is something weird to do with the fact that you are doing a string check for true/false despite it theoretically maybe working, I would try resolving that issue and seeing if it fixes your problem





also get in the habit of adding echo statements everywhere when debugging something that doesn't make sense so you know exactly where your code breaks down, I doubt that the if(%currentMoney < %nextPick) is the actual problem, and the fact that you are dwelling on this single condition is probably preventing you from finding the real issue, when debugging code: account for everything and never assume that the issue is a single thing because sometimes or often times, it turns out that the actual issue in your code is something you didn't account for
« Last Edit: August 12, 2016, 04:53:15 AM by Swollow »

Quote
please do not check true/false values using $=
the $= operator is a string equals
torquescript treats true or false as 1 or 0

When I make $buy false
Code: [Select]
if($buy $= true)
    {
BuyPick();
    }

doesn't run

and also I was using $buy for a function loop and didn't get rid of the variable

I'll try changing the true/false anyway

Instead of:

if(%currentMoney < %nextPick) // This if is definitely the problem
{
   $buy = false;
}
if($buy $= true)
{
   BuyPick();
}


You can structure your if statements in a more logical manner:

//Is the $buy variable true?
if($buy)
{
   //Do we have enough to buy a pick?
   if(%currentMoney >= %nextPick)
   {
      //We have enough, buy the pick
      buyPick();
   }
   else
   {
      //We don't have enough.. set buy to false?
   }
}