Author Topic: Basic ServerCmd and Messages  (Read 2042 times)

I am very new to TorqueScript (in fact, I just started learning today), and I'd like some help with this basic script I've been trying to make.
Pretty much I am trying to make it so that if the client types "/messageall {message}", a chat message is displayed to all users, and if the client types "/messageuser {user blid} {message}" then a message is sent to a specific user.
Code: [Select]
function servercmdMessageAll(%client, %message)
{
if (!%client.issuperadmin) return;
messageAll(%message);
}

function servercmdMessageUser(%client, %blid, %message)
{
%target = getclientbyid(%blid);
if (%target == -1) return;
messageClient(%target, "", "<color:FF0000>Message from [" @ %client @ "] <color:FFFFFF>" @ %message");
}
Unfortunately, this code does not run. Even when trying simple commands like talk(); it still does not display.
I'd greatly appreciate any help (even more so if well explained), even if this script is utter nonsense.
Thanks!

messageAll('', %msg);
messageClient(%target, '', %msg);

you can also use if(isObject(%target)) instead of just checking for -1.

Replace getclientbyid(%blid) with findClientByBL_ID(%blid) because getClientByID does not exist.
Replace if(%target == -1) to if(!isObject(%target)) because the command above returns a 0, not a -1 (I'm probably wrong) but, it's better to use !isObject(object) in most situations.
Also, replace messageAll(%message); with messageAll('', %message);, the '' are not quotes. They are apostrophes. The above post explains the actual use, they are supposed to be tagged strings.
You have a syntax error at ");, just remove the quote.

Everything else looks fine to me, although, you're only going to be able to say 1 word to the player.

Edit: In the messageClient line use %client.getPlayerName() instead of %client because %client is an object so you'll just get a number.
« Last Edit: March 27, 2016, 07:04:32 PM by Kyuande »

messageAll('', %msg);
messageClient(%target, '', %msg);

you can also use if(isObject(%target)) instead of just checking for -1.
Ah, thanks for the heads up

Replace getclientbyid(%blid) with findClientByBL_ID(%blid) because getClientByID does not exist.
Replace if(%target == -1) to if(!isObject(%target)) because the command above returns a 0, not a -1 (I'm probably wrong but--) and it's better to use !isObject(object) in most situations.
Also, replace messageAll(%message); with messageAll('', %message);, the '' are not quotes. They are apostrophes. The above post explains the actual use, they are supposed to be tagged strings.
You have a syntax error at ");, just remove the quote.

Everything else looks fine to me, although, you're only going to be able to say 1 word to the player.
Greeat, thanks! I'll look into those mistakes I've made, and see what I can do about multiple words!

Thanks for the help, I'll get back to you later!

Here's an example of multiple words:
Code: server.cs (10 lines)
function serverCmdHi(%client, %msg0, %msg1, %msg2, %msg3, %msg4, %msg5, %msg6)
{
    for(%i = 0; %i <= 6; %i++) //There are 7 msg arguments, start from 0, go to 6.
        if(%msg[%i] !$= "") //If the current number has something in it, add it to the message variable
            %msg = %msg SPC %msg[%i]; //Each argument gets a space in between

    %client.chatMessage("\c6Hello. I have a message for you: \c3" @ %msg); //%client.chatMessage(string message);
    //\c0 to \c8 have different colors, you can do a /colorTest on your server for each of their colors.
    //Ex: <color:ffff00> is the same as \c3 (Yellow)

}

« Last Edit: March 27, 2016, 07:17:00 PM by Kyuande »

Here's an example of multiple words:
Code: server.cs (10 lines)
function serverCmdHi(%client, %msg0, %msg1, %msg2, %msg3, %msg4, %msg5, %msg6)
{
    for(%i = 0; %i <= 6; %i++) //There are 7 msg arguments, start from 0, go to 6.
        if(%msg[%i] !$= "") //If the current number has something in it, add it to the message variable
            %msg = %msg SPC %msg[%i]; //Each argument gets a space in between

    %client.chatMessage("\c6Hello. I have a message for you: \c3" @ %msg); //%client.chatMessage(string message);
    //\c0 to \c8 have different colors, you can do a /colorTest on your server for each of their colors.
    //Ex: <color:ffff00> is the same as \c3 (Yellow)

}

Thanks for the example; I was able to improve my code to something like this:
Code: [Select]
//Command "/messageall {message}"
function servercmdMessageAll(%client, %msg1, %msg2, %msg3, %msg4, %msg5, %msg6, %msg7, %msg8)
{
if (!%client.issuperadmin) return;
for (%i = 0; %i < 8; %i++)
if (%msg[%i] !$= "")
%msg = %msg SPC %msg[%i];
messageAll('', "[Announcement] \c6" @ %msg);
}

//Command "/messageuser {name} {message}"
function servercmdMessageUser(%client, %name, %msg1, %msg2, %msg3, %msg4, %msg5, %msg6, %msg7, %msg8)
{
%target = findClientByName(%name);
if(!isObject(%target))
messageClient(%client, '', "\c0Could not send message to user [\c3" @ %name @ "\c0]"); //1
return;
for (%i = 0; %i < 8; %i++)
if (%msg[%i] !$= "")
%msg = %msg SPC %msg[%i];
messageClient(%target, '', "\c0Message from [\c3" @ %client.name @ "\c0] \c6" @ %msg); //2
}
An error I seem to be having now is that if I have the line of code marked //1, then the code marked //2 stops functioning.

This part:
if(!isObject(%target))
      messageClient(%client, '', "\c0Could not send message to user [\c3" @ %name @ "\c0]"); //1
      return;

There needs to be brackets ({}) if you're going to have more than one item, otherwise it's just going to return no matter what.

Looks good to me otherwise!

You'll want to change your for statements to:
for (%i = 1; %i <= 8; %i++)
Right now it cycles through 0-7. But you don't have a %msg0 variable and it's not getting to the %msg8 variable. This will cycle through 1-8.

I also recommend changing
%msg = %msg SPC %msg[%i];
to
%msg = %msg @ %msg[%i] @ " ";
Or else there will always be a space at the start of the message.

It's also better practice to use %client.getPlayerName() rather than %client.name.

I also recommend changing
%msg = %msg SPC %msg[%i];
to
%msg = %msg @ %msg[%i] @ " ";
Or else there will always be a space at the start of the message.
We have a trim function, too, if you prefer something a bit more readable code-wise.
%msg = trim(%msg SPC %msg[%i]);

Thanks for all the help, guys!
I managed to update and improve the code successfully, limiting it to just one command (/message) and (/message *) instead of (/messageAll)

I've tested and modified it and it's all working fine!

So for the loop I've made the mistake according to the boys above, it's better to do something like this:

for (%i = 0; %i < 8; %i++)
   if (%msg[%i] !$= "")
   {
      if(%msg $= "") //Check if the entire string has any value, if not, let's create it
         %msg = %msg[%i];
      else //Add onto the entire string
         %msg = %msg SPC %msg[%i];
   }

%msg = stripMLControlChars(trim(%msg)); //Remove any weird spaces and torque ml characters like <color:ffffff>

I feel like it's related enough to this thread to bump rather than make a new thread just for this:

messageClient(%client, '', "...");
messageAll('', "...");


In these, what sort of argument would go inside ''?



Also, how did you get this beautiful formatting, assuming it was partly automated?
Here's an example of multiple words:
Code: server.cs (10 lines)
function serverCmdHi(%client, %msg0, %msg1, %msg2, %msg3, %msg4, %msg5, %msg6)
{
    for(%i = 0; %i <= 6; %i++) //There are 7 msg arguments, start from 0, go to 6.
        if(%msg[%i] !$= "") //If the current number has something in it, add it to the message variable
            %msg = %msg SPC %msg[%i]; //Each argument gets a space in between

    %client.chatMessage("\c6Hello. I have a message for you: \c3" @ %msg); //%client.chatMessage(string message);
    //\c0 to \c8 have different colors, you can do a /colorTest on your server for each of their colors.
    //Ex: <color:ffff00> is the same as \c3 (Yellow)

}

« Last Edit: April 02, 2016, 09:54:32 PM by Teneksi »

Yeah, Zeblote made the package for Sublime Text

For the '', it's a tag, look up tags, common ones used:
'MsgAdminForce'
'MsgClearBricks'

There are tons more that I can't think of right now, they send commands to the clients to handle, but also used for creating sound to the client.
« Last Edit: April 02, 2016, 11:58:22 PM by Kyuande »

I feel like it's related enough to this thread to bump rather than make a new thread just for this:

messageClient(%client, '', "...");
messageAll('', "...");


In these, what sort of argument would go inside ''?

It's a tagged string to run a special message callback on the client side. For example, if you use 'MsgAdminForce' then clients will hear the sound of someone getting admin.

These are all the default ones:

'AddClientToTeam'
'AddTeam'
'InitTeams'
'MsgAdminForce'
'MsgClearBricks'
'MsgClearInv'
'MsgClientInYourMiniGame'
'MsgClientJoin'
'MsgConnectionError'
'MsgDeEquipInv'
'MsgDropItem'
'MsgEquipInv'
'MsgError'
'MsgHilightInv'
'MsgItemPickup'
'MsgPlantError'
'MsgPlantError_Buried'
'MsgPlantError_Float'
'MsgPlantError_Flood'
'MsgPlantError_Limit'
'MsgPlantError_Overlap'
'MsgPlantError_Stuck'
'MsgPlantError_Teams'
'MsgPlantError_TooFar'
'MsgPlantError_TooLoud'
'MsgPlantError_Unstable'
'MsgProcessComplete'
'MsgSetInvData'
'MsgStartTalking'
'MsgStopTalking'
'MsgUploadEnd'
'MsgUploadStart'
'MsgYourDeath'
'MsgYourSpawn'
'RemoveClientFromTeam'
'RemoveTeam'
'SetTeamCaptain'
'SetTeamName'


I don't know what most of them are for though.

The teams are probably from slayer.

Sounds: AdminForce, ClearBricks, UploadStart, UploadEnd, ProcessComplete
ConnectionError I'm guessing is the leave sound?
These sounds are used to accompany messages, like '___ connected.' '___ has banned ___ for x minutes. (Reason: ____)', '____ is uploading a save file.' etc

ClientInYourMiniGame makes their name blue/black in the F2 window. Could be a possible arg passed to set it on or off.
Not sure what Error is.
Not sure what the generic PlantError is, but the rest make the plant errors show up on your screen. Not sure why they're messages.
Start/Stop talking update the small blue names in the top left corner of the screen. Not sure why these are messages.
YourDeath/YourSpawn are to inform your client when you're dead or alive. I'm not sure what effect this has, nor why they're messages.
ItemPickup and DropItem are related to the Q menu. These are only messages because TGE by default informs you whenever you drop or pickup an item in chat. Though it could've been moved to a clientcmd, since we don't use the messages.
The callbacks with Inv in their names are related to the brick cart. Still, very unsure why these are messages.