Author Topic: Check Distance  (Read 464 times)

So I'm making a small script that checks the distance between two players, but it's saying that I have a syntax error but I can't find it.
Code: [Select]
package DistanceCheck
{
function serverCmdDistance(%client, %targ1, %targ2)
{if(%client.isadmin==1){
%targ1=fcbn(%targ1).name();
%targ2=fcbn(%targ2).name();
%targ1.player.getposition(%xA %yA %zA);
%targ2.player.getposition(%xB %yB %zB);
//3D Distance Formula
%Val = sqrt(%xA-%xB^2+%yA-%yB^2+%zA-%zB^2);
%client.chatmessage("\c2The distance between \c3"@%targ1@"\c2 and \c3"@%targ2 "\c2 is: \c0"@%Val);}
}
activatepackage(DistanceCheck);

Help?
It also crashes Blockland for some odd reason.

VectorDist(Vector3f a, Vector3f b)..?

Code: [Select]
%targ1.player.getposition(%xA %yA %zA);
%targ2.player.getposition(%xB %yB %zB);
You don't need a package for any of this. The fact that you have three variables right next to each other is what's causing the (first) syntax error. Additionally, getPosition() has no arguments, so any variables there won't do anything. You also have parantheses around name, but name on a client is a function, not a variable. You are trying to get the position of the player of a string of text, which will not work since clients are not named like they were in v002. Additionally, you did not check if the clients and their players existed, which will cause console errors if a nonexistant client/player is targeted.

You cannot use carrots for exponents, you have to actually multiply the numbers. Use vectorDist like Resonance said.

Here's a working code.
Code: [Select]
function serverCmdDistance(%client, %targ1, %targ2)
{
   if(%client.isadmin)
   {
      %targ1=findClientByName(%targ1);
      %targ2=findClientByName(%targ2);
      if(isObject(%targ1.player) && isObject(%targ2.player))
      {
         %pos1 = %targ1.player.getposition();
         %pos2 = %targ2.player.getposition();
      }
      %dist = vectorDist(%pos1, %pos2);
      messageClient(%client, '', "\c2The distance between \c3" @ %targ1.name @ "\c2 and \c3" @ %targ2.name @ "\c2 is: \c3" @ %dist @ "\c2.");
   }
}

Thanks, although you didn't have to do it for me.
Locking.