Jump to content

ChatMsg


nikitafloy

Recommended Posts

How can I send a message after you specify 'id'? If I enter a variable, it takes the value to any sign within.

Example :/ n 0 Hi, I know what you did last summer!

Output: , Hi

function writetoID( player, command, id, text ) 
    local plName = getElementByID ( 'ID' .. tostring(id) ) 
    if plName == nil then 
        outputChatBox ("Нет игрока под этим ID.", player ) 
    else 
        local myName = tostring(getPlayerName(plName)) 
        local r,g,b = getPlayerNametagColor(plName) 
        local rn = string.format("#%02X%02X%02X", r,g,b) 
        source = player 
        local text = tostring(text) 
        sendMSG( rn..''..myName..'#ffffff, ' ..text, 0 ) 
    end 
end 
addCommandHandler( 'n', writetoID ) 

Link to comment

you are using ´text´ as a variable, but in this case ´text´ is a table. You have to use ´concat´. Implent this in your code;

sendMSG ( rn .. ' ' .. myName .. '#ffffff, ' .. table.concat ( text, ' ' ), 0 ) 

The description from 'concat' reads;

Concatenate the elements of a table together to form a string. Each element must be able to be coerced into a string. A separator can be specified which is placed between concatenated elements. Additionally a range can be specified within the table, starting at the i-th element and finishing at the j-th element.

source; http://lua-users.org/wiki/TableLibraryTutorial

Link to comment

Try this:

function writetoID( player, command, id, ... ) 
    local plName = getElementByID ( 'ID' .. tostring(id) ) 
    if plName == nil then 
        outputChatBox ("??? ?????? ??? ???? ID.", player ) 
    else 
        local myName = tostring(getPlayerName(plName)) 
        local r,g,b = getPlayerNametagColor(plName) 
        local rn = string.format("#%02X%02X%02X", r,g,b) 
        local text = table.concat ( { ... }, " " ) 
        source = player 
        sendMSG( rn..''..myName..'#ffffff, ' ..text, 0 ) 
    end 
end 
addCommandHandler( 'n', writetoID ) 

Edit:

@tosfera: that won't work, text is not a table but a string, and every word is passed as a different argument to the handler function of the command, to catch all these arguments, you have to use '...' (three dots).

Edited by Guest
Link to comment
Try this:
function writetoID( player, command, id, ... ) 
    local plName = getElementByID ( 'ID' .. tostring(id) ) 
    if plName == nil then 
        outputChatBox ("??? ?????? ??? ???? ID.", player ) 
    else 
        local myName = tostring(getPlayerName(plName)) 
        local r,g,b = getPlayerNametagColor(plName) 
        local rn = string.format("#%02X%02X%02X", r,g,b) 
        local text = table.concat ( arg, " " ) 
        source = player 
        sendMSG( rn..''..myName..'#ffffff, ' ..text, 0 ) 
    end 
end 
addCommandHandler( 'n', writetoID ) 

your code won't work, why? How about this mistake you made;

local text = table.concat ( arg, " " ) 

'arg' isn't a parameter of his function, it's 'text'. :)

Link to comment

Have you tried it? ;)

arg == { ... } 

Edit:

I've read after, just for you. I forgot that the hidden variable 'arg' has an extra value, wich is the number of the arguments passed to the function. So using that maybe wasn't a good idea, there might be an extra number at the output.

Link to comment
Have you tried it? ;)

arg == { ... } 

Edit:

I've read after, just for you. I forgot that the hidden variable 'arg' has an extra value, wich is the number of the arguments passed to the function. So using that maybe wasn't a good idea, there might be an extra number at the output.

That's true, and you comment at mine... I think we are even at the mistakes we made now... :lol::lol:

Link to comment

Mine would work perfectly except for that extra output, but I've corrected that. Yours wouldn't work at all. So there are different between mistakes, but I admit, that I also made a small one. :)

Oh, and there wouldn't be any extra number at the output, since the number of the arguments isn't an indexed part of the table but it has a name 'n', and table.concat concatenate only indexed values.

Edited by Guest
Link to comment
Mine would work perfectly except for that extra output, but I've corrected that. Yours wouldn't work at all. So there are different between mistakes, but I admit, that I also made a small one. :)

Haha what is that smell... is it the smell of a scripting challenge who makes the least mistakes in their scripts? If so...

1838_picture.png

Link to comment
Sorry, just won the challenge by my edit. No offense. ;)

just a bit of finetuning, because there was some crap in there;

addCommandHandler ( 'n', 
    function ( thePlayer, _, id, ... ) 
        local receiver = getElementById ( 'ID' .. tostring ( id ) ); 
        if ( receiver ) then 
            local r, g, b = getPlayerNameTagColor ( receiver ); 
            sendMSG ( string.format ( "#%02X%02X%02X", r, g, b ) .. getPlayerName ( receiver ) .. "#FFFFFF, " .. table.concat ( { ... }, " " ), 0 ); 
        else 
            outputChatBox ( "Нет игрока под этим ID.", thePlayer ); 
        end 
    end 
); 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...