Jump to content

Guest

Recommended Posts

Posted (edited)

Hey guys i'm new in scripting, i used wiki to learn

i have made this code just to test myself

addEventHandler("onPlayerChangeNick", getRootElement(), wasNickChangedByUser)
function changenick (onPlayerChangeNick)
outPutChatBox ( " Your have been changed your nickname " )
end

How can i fix it ?, ik it's cancer

======================================================================

after some time because no one answered

I tried this code and it's worked 

addEventHandler('onClientPlayerChangeNick', root,
	function(oldNick, newNick)
outputChatBox('You have been changed your nickname', 255, 100, 100)
end)

But what's is  " root, "

and why in some script there is

function testmod (oldNick, newNick)
 

And other script only

	function(oldNick, newNick)

 

Edited by Guest
Posted

The event handler has to be under the fuction if you don’t include the function directly to the handler like in the working script you shown. A computer reads code from up to bottom so you’re just calling a function that doesn’t exist for the computer, which is solved by putting the addeventhandler under the function 

If I helped you, make sure you click the reaction button on the right to support me!
It will grant me more visibility and will help me to help others!

If you have any kind of request or subject you would like to discuss about with me,
don't hesitate to send me a private message about it!

 

  • Moderators
Posted (edited)

Root is the top of the elements.(it is an element)

https://wiki.multitheftauto.com/wiki/Element_tree

Tre.png

Which represents the container of everything. If used in an addEventHandler, every element ingame is capable to trigger/activate it.

 

More information about specifying the trigger range:

 

Edited by IIYAMA

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted

Also make sure if your function is called changenick to use the same name in the handler  and not something like wasNickChangedByUser

If I helped you, make sure you click the reaction button on the right to support me!
It will grant me more visibility and will help me to help others!

If you have any kind of request or subject you would like to discuss about with me,
don't hesitate to send me a private message about it!

 

Posted (edited)
3 hours ago, Galactix said:

Also make sure if your function is called changenick to use the same name in the handler  and not something like wasNickChangedByUser

When nesting a function in an event handler like this, don't use the function name.  Just do the following.

addEventHandler("onPlayerChangeNick", getRootElement(),wasNickChangedByUser)--wasNickChangedByUser is the function name.
function wasNickChangedByUser()--removed onPlayerChangeNick, within these brackets you only pass returns from event handlers, functions etc.
	outPutChatBox ( " Your have been changed your nickname " )
end

oldNick and newNick are what this event handler returns, an event handler is just something that happens when a player does a certain action (or event) and it handles that event with the function.  So when the function happens the event handler passes the players old name, and new name to the function.  Make sure you check the returns part of wiki page.  Hope that made it a bit clearer to you. :)

Edited by kieran

 

 

Posted (edited)
18 minutes ago, kieran said:

When nesting a function in an event handler like this, don't use the function name. 

Which is what I said in my first post.

Edited by Galactix

If I helped you, make sure you click the reaction button on the right to support me!
It will grant me more visibility and will help me to help others!

If you have any kind of request or subject you would like to discuss about with me,
don't hesitate to send me a private message about it!

 

Posted

@GalactixI read his script wrong, I thought the bracket was at the end of the function...  Nesting is the second example he gave on original post, where you just put the word function under or beside the element that is triggering handler (separated by a comma of course), and then you add a closing bracket at the end of your function, below it or as most do (as it's proper way), you add it to the end of the last end in that function....  If that makes sense.  I forgot to take that line out my post when I edited it. xD

  • Haha 1

 

 

Posted

Oh alright, I get what you meant now, but I wasn't talking of the nesting in my second post, which is why you were confused.

If I helped you, make sure you click the reaction button on the right to support me!
It will grant me more visibility and will help me to help others!

If you have any kind of request or subject you would like to discuss about with me,
don't hesitate to send me a private message about it!

 

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