Jump to content

Anyone see a problem with this?


Osidog

Recommended Posts

Hi. I'm learning the basics.

I have reading through the pages at

http://robhol.net/guide/basics/

https://wiki.multitheftauto.com/index.ph ... putChatBox

I was able to start basic commands and all was going well. I then scrapped my lua script and tried making some of my own. Although none of them would work. So i tried even the very basic commands and they wouldn't work. This is one of the commands that's not working.

function SayHello(player, command)
outputChatBox("Hello Everyone")
end
 
addEventHandler("hi", SayHello)

I'd expect this code to add the text "Hello Everyone" to the chatbox when I type /hi

nothing happends.

have i made a mistake somewhere?

Link to comment
function SayHello(player, command)
outputChatBox("Hello Everyone")
end
 
addCommandHandler("hi", SayHello)

use addCommandHandler for commands and addEventHandler for events.

Thank you Ryder. :)

One more question. How does one send a message to one player.

I know using outputChatBox sends the message to everyone. Is there a function/element which allows me to send a message to just one person? I've been searching around for it but can't find it.

Link to comment
Use wiki a lot for reference.

https://wiki.multitheftauto.com/index.ph ... putChatBox

Look at the parameters. 2nd parameter is visibleTo.

As you may already know, command handler function's 1st parameter is the player that entered the command, so you that to output text for him.

outputChatBox( "hello world", player );

Thanks 50p.

I'm slowly getting the hang of it. I keep making lots of mistakes and there's still a lot I don't understand. but all will come to me eventually if I keep trying. Your help is invaluable to me whilst i learn. I appreciate it. :)

If i'm right them, adding the visibleTo will allow me to send to just one player.

function PersonalMessage(sender, sendto, message)
-- sends a private message to one person
recipient = getPlayerFromName(sendto)
outputChatBox("PM from" .. sender ":" .. tostring(message), element visibleTo=recipient )
end
addCommandHandler("pm", PersonalMessage)

I'm going to test this out. Hoping it will work for me. I'd like to add some error checking which searches for the recipients name to ensure someone's in the server with that name, and if there isn't it will send a message to the senders telling them so.

Edited by Guest
Link to comment

You don't do it that way:

element visibleTo=(recipient)

Wiki tell you what are default arguments by using =. You just use the element that will receive the text. Therefore:

outputChatBox("PM from" .. sender ":" .. tostring(message), recipient )

Remember to make sure the returned value from getPlayerFromName is not false. Like:

if recipient then
outputChatBox("PM from" .. sender ":" .. tostring(message), recipient )
end

If you don't do that, you will get warning/error messages.

Also, login as admin and use the debug window by using debugscript command:

/debugscript 3
Link to comment
You don't do it that way:
element visibleTo=(recipient)

Wiki tell you what are default arguments by using =. You just use the element that will receive the text. Therefore:

outputChatBox("PM from" .. sender ":" .. tostring(message), recipient )

Remember to make sure the returned value from getPlayerFromName is not false. Like:

if recipient then
outputChatBox("PM from" .. sender ":" .. tostring(message), recipient )
end

If you don't do that, you will get warning/error messages.

Also, login as admin and use the debug window by using debugscript command:

/debugscript 3

Ok. I think I understand.

i will go test this out.

Link to comment
function PersonalMessage(sender, sendto, message)
-- sends a private message to one person
recipient = getPlayerFromName(sendto)
outputChatBox("PM from" .. sender ":" .. tostring(message), element visibleTo=recipient )
end
addCommandHandler("pm", PersonalMessage)

Uhm, there is a mistake here:

The variable sender is a player element, and you can not put it into a string.

And the variable sendto is also wrong, cuz there need to be cmd. A better example of the code above:

-- wiki says: player playerSource, string commandName, [string arg1, string arg2, ...]
-- After commandName you can put the arguments of the command. In this case the name you want to sent to and a message
function personalMessage(sender, cmd, sendTo, message)
-- You see that there are in total 4 arguments needed in other to make the command work.
-- Now we get the recpient
recipient = getPlayerFromName(sendTo);
-- Now we get the player name of the sender
senderName = getPlayerName(sender);
-- Now we are going to output the message into the chatbox
outputChatBox("pm from " .. senderName .. ": " .. message, recipient);
-- Now this command is working.
end
-- Add the command handler, i guess you know how that works.
addCommandHandler('pm', personalMessage);

A full working pm command.

Link to comment

Ok. Not quite there just yet. I've logged into admin and opened up the debug window. I've tried sending a PM to myself. (that should be possible?)

Although nothing shows up in the chat box or the debug window.

I entered "/pm osidog test123"

I've also added the resource to start with the server & have tied restarting it.

function PersonalMessage(sender, sendto, message)
-- sends a private message to one person
  	recipient = getPlayerFromName(sendto)
if recipientthen
outputChatBox("PM from" .. sender ":" .. tostring(message), recipient )
end
end
addCommandHandler("pm", PersonalMessage)

Link to comment
function PersonalMessage(sender, sendto, message)
-- sends a private message to one person
recipient = getPlayerFromName(sendto)
outputChatBox("PM from" .. sender ":" .. tostring(message), element visibleTo=recipient )
end
addCommandHandler("pm", PersonalMessage)

Uhm, there is a mistake here:

The variable sender is a player element, and you can not put it into a string.

And the variable sendto is also wrong, cuz there need to be cmd. A better example of the code above:

-- wiki says: player playerSource, string commandName, [string arg1, string arg2, ...]
-- After commandName you can put the arguments of the command. In this case the name you want to sent to and a message
function personalMessage(sender, cmd, sendTo, message)
-- You see that there are in total 4 arguments needed in other to make the command work.
-- Now we get the recpient
recipient = getPlayerFromName(sendTo);
-- Now we get the player name of the sender
senderName = getPlayerName(sender);
-- Now we are going to output the message into the chatbox
outputChatBox("pm from " .. senderName .. ": " .. message, recipient);
-- Now this command is working.
end
-- Add the command handler, i guess you know how that works.
addCommandHandler('pm', personalMessage);

A full working pm command.

Thanks Alexander.

I'll go and test it again now after making the adjustments. One more thing though. I don't quite understand why "cmd" has to be included, or what it does. Is it the same as using "command"? Could you explain that to me please.

Thanks. :D

edit/

Alexander you're right. That's working! :) Just need to try and understand the code a bit more, but i'm getting there, slowly but surely. :)

Thanks.

Link to comment

Argument names in lua are not significant - it's the order in which they're passed that matters. When a command is entered, its handler function will be called like this: someFunction( sender, commandName, argument1, argument2, ... )

The cmd has to be there, even if you don't use it, simply because that's how MTA calls the function. If it's not there, you'll get all arguments "shifted" and each variable will contain something quite different than what you expected.

If this doesn't make sense, wait for a few hours and I'll try to make it clearer.. I'm sleepy. :mrgreen:

Link to comment
I don't quite understand why "cmd" has to be included, or what it does. Is it the same as using "command"? Could you explain that to me please.

You can name those variables anything you like. So if you call it "cmd", "command" or "cheese", it'll all contain the same thing.

The second (note, THIS is what matters: the order, not the name :P) argument of a command handler always contains the command entered. I.e.

Player entered /boo a b

First argument: player [element]

Second argument: boo [string]

Third argument: a [string]

Fourth argument: b [string]

Hope that clears it up a bit ;)

EDIT: Robhol beat me to it, but if I compare the 2 posts I personally think mine is a bit more easy to understand. :P

Link to comment

The point is that you might want to have multiple command handlers going through one function. You can them use the 'cmd' variable to tell which one is actually being called. In general usage, you can ignore it.

Link to comment

OK, i'm a bit confused still but it is getting late here in England. I get that the argument names aren't significant although the order of them is. I understand that now.

Now With command handlers, the first argument is always the player name who entered the command, the second is the argument that is required for mta to call the function & the rest are whatever you want them to be, although the arguments are only needed if you need to grab extra data from whoever entered them? For example the recipients name and the message.

So let's say i make the function

function GoogleSearch ( )
outputChatBox("Google.com search engine")
end
 
addCommandHandler("google", GoogleSearch)

I haven't included any arguments, and it's worked. I was really confused as to what arguments I include and in which order, although i think i'm starting to understand more. Although there's still a few things i've come across that i don't understand, such as when and why i may sometimes need to add getRootElement to the addEventHandler or the source in the [/b]outputChatBox[/b] - see this example.

https://wiki.multitheftauto.com/index.ph ... PlayerJoin

It'd be nice if someone could also explain that, although I don't want to pester you all with questions as i'm sure to have lots and if I be honest I've not searched as much as i should as it's getting late.

I'm going to get some shuteye and will carry on with this tomorrow. Also If i'm wrong with the above please correct me.

Thanks everyone for all your help. :)

Link to comment

I give an example about the seccond argument 'cmd':

-- make the function
function moneyCmd(player, cmd, ammount)
if cmd == "giveMoney" then
if ammount ~= nil then
           ammount  = tonumber(ammount);
 
givePlayerMoney(player, ammount);
else
outputChatBox("[usage] /givemoney [ammount]", player);
end
else if cmd == "takeMoney" then
if ammount ~= nil then
           ammount = tonumber(ammount);
 
takePlayerMoney(player, ammount);
else
outputChatBox("[usage] /takemoney [ammount]", player);
end
end
end
 
addCommandHandler("givemoney", moneyCmd);
addCommandHandler("takemoney", moneyCmd);

Thats how to use the seccond argument, you can assign multiple commands to a functions. And you can see wich command they use with the seccond argument.

Link to comment

Alexander that's a great example. Thanks.

eAi I've read those links. It's a lot to take in. I think i'm ok with source. Going to try it in a command to test it out myself.

The 'getRootElement i'm still a bit confused after reading the two links. I'm going to read them again see if i can understsand it a bit more.

I've been trying to work on that PM script and have been running into a few problems. I'll post the script in here, although I've got a few more things i'll like to try with it before then.

Thanks.

Link to comment

Indeed, understanding the element tree and it's relation to the event system can be confusing at first - especially in relation to the "getPropagated" bool.

But the best way of understanding it (after you understand the element tree) is that events can be triggered on any element. Events can be handled (or captured) on the element they were triggered on - or any Parent or Child of that element IF the getPropagated bool is true. (it is by default if you don't specify it.) Events propagate to parents and children, but not to branches.

As an example, say an event is triggered on a player. All players are children of the Root Element. So are all vehicles, but they are a seperate branch. If you attach a Handler to the root element (with propagated true) then the event will fire on any player or vehicle recieving the event. If you attach a handler to a specific player, it will fire when the event is triggered for that player, or a higher element (such as root), or a lower element (for example, if you assigned every player a "weapon" element), but not other players - as they are each a branch of the root element. It would also never be triggered if the event is fired on a vehicle. (vehicles are also within seperate element branches.)

With getPropagated set false, the event is only recieved if the event is fired at that specific element. This is usually preferred when working with GUI elements, (did the client click this specific button, for example) so bear it in mind.

When dealing with players in the server, it usually boils down to "do I want to do something on every player, or just on a specific player?" - In the case of every player, you would attach to the root element, otherwise you would attach to just the player element you are interested in.

The hidden variable "source" plays into the propagation of events. It contains the element the event was triggered by. If getPropagated is false, it's not going to be particularly important - the source will be the element you attached the handler to. However, it matters otherwise: if you attached a Handler to the Root Element, but you need to know which player triggered the event for example (e.g. for "onPlayerChangeNick", you attached to the Root Element to catch anyone changing their nickname, but who actually was it that just changed their nickname?) then the source variable will contain that element, and you can run code on them. (e.g. output a confirmation message with outputChatBox(source, "Confirm Message") (serverside only :P) )

Sorry, that's quite a wall of text, and it's probably very dry. I hope you can make some sense of it though :P

Link to comment

This is the script I was practising on. I'd say it's complete now. I was testing it and found a few problems but I think they're all solved now.

It's given me an understanding how the scripts are made, and it has been a good starting point for me to start learning LUA. I'd like to thank you all for the support given. It's been very helpful and i doubt i would have been able to do it without the support and ofcourse the wiki. I'm going to keep reading the wiki and try and make some more scripts. I feel as if i'm getting the hang of it, but still got a long way to go.

The script isn't a big one but I've literally spent hours and hours working on it, mainly experimenting, trying different things and lots of testing but it seems to be working ok now.

Here's the script, if you'd like to have it or just want to look.

Thanks All. :D

function personalMessage(sender, cmd, sendTo, ... ) 
-- Find the player to send the message too and also get names of both players.
recipient 		= getPlayerFromName(sendTo);
recipientName	= getPlayerName(recipient); 
senderName		= getPlayerName(sender); 
-- Pack all words of message into a table
tbl_Message	 = {...}
-- Join all extra arguments into a string to form message
theMessage = table.concat( tbl_Message, " " );
 
-- Check whether the recipient has been found and senders message exist.
if recipient ~= false and #tbl_Message > 0 then 
-- No errors are found so send message to recipient and a confirmation to sender
outputChatBox("PM from " .. senderName .. ": " .. theMessage, recipient, 255, 50, 0);
outputChatBox("PM successfully sent to " .. recipientName, sender, 255, 50, 0);
else
-- There was a problem with the message. Send a notice to the sender.
outputChatBox("PM not sent. Check format /pm full_name message", sender, 255, 50, 0)
end
end
addCommandHandler("pm", personalMessage); -- command handler...

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...