fairyoggy Posted May 19, 2021 Share Posted May 19, 2021 How to press a button with using a command? I mean, not an ordinary click on the gui (onClientGUIClick) when the player presses the button. I mean how to do with addCommandHandler to make the button click Example: addEventHandler("onClientGUIClick", root, function() if source == button1 then outputChatBox ("HI", 231,217,176,false ) end end) function zxcs() -- text for using button end addEvent("zxcs", true) addEventHandler( "zxcs", localPlayer, zxcs ) addCommandHandler ( "z", zxcs ) I want when the player writes in the chat /z the button must be pressed Link to comment
Spakye Posted May 19, 2021 Share Posted May 19, 2021 (edited) Hello, idk if it will work but you can try with triggerEvent() Edited May 19, 2021 by Spakye 1 Link to comment
fairyoggy Posted May 19, 2021 Author Share Posted May 19, 2021 1 minute ago, Spakye said: Hello, idk if it will work but you can try with triggerEvent() How can I call the button in this way? Link to comment
SpecT Posted May 19, 2021 Share Posted May 19, 2021 Just now, oggygod said: How can I call the button in this way? You can call the function which handles the onClientGUIClick event. 1 Link to comment
fairyoggy Posted May 19, 2021 Author Share Posted May 19, 2021 2 minutes ago, SpecT said: You can call the function which handles the onClientGUIClick event. Can you please give an example of this action in the code? Link to comment
Spakye Posted May 19, 2021 Share Posted May 19, 2021 (edited) local myButton = -- your button code addEventHandler("onClientGUIClick", myButton, myFunction, false) -- this event call the function when the button is clicked function myFunction() -- what to do when the button is clicked end addCommandHandler ( "z", myFunction ) Edited May 19, 2021 by Spakye 1 Link to comment
fairyoggy Posted May 19, 2021 Author Share Posted May 19, 2021 14 minutes ago, Spakye said: local myButton = -- your button code addEventHandler("onClientGUIClick", myButton, myFunction, false) -- this event call the function when the button is clicked function myFunction() -- what to do when the button is clicked end addCommandHandler ( "z", myFunction ) local mybutton = guiCreateButton(1000, 200, 75, 30, "ACC", false) addEventHandler("onClientGUIClick",mybutton,myFunction,false) function myFunction() outputChatBox ("test", 231,217,176,false ) end addCommandHandler("z",myFunction) I misunderstood something? Using the command /z I get the text in the chat (test) but when I press the button. Nothing happens I just wrote a button click differently in the code(1 post in this topic), and now I don't know how to write it correctly. Link to comment
Spakye Posted May 19, 2021 Share Posted May 19, 2021 Im not sure why it doesnt work, any errors in debug? 1 Link to comment
fairyoggy Posted May 19, 2021 Author Share Posted May 19, 2021 2 minutes ago, Spakye said: Im not sure why it doesnt work, any errors in debug? not errors in debug Link to comment
Spakye Posted May 19, 2021 Share Posted May 19, 2021 I tried the code and i have an error, the problem is the function comes after the handler, place the function before. 1 Link to comment
fairyoggy Posted May 19, 2021 Author Share Posted May 19, 2021 2 minutes ago, Spakye said: I tried the code and i have an error, the problem is the function comes after the handler, place the function before. OK, thank you. Everything works with your code. But I'm trying to use my button, which is in another window, and I can't get it to work. function window() window = guiCreateWindow((sW - 406) / 2, (sH - 461) / 2, 455, 465, "Window", false) windowbutton1 = guiCreateButton(330, 432, 118, 22, "butt", false, window) end function myFunction() outputChatBox ("test", 231,217,176,false ) end addCommandHandler("z",myFunction) addEventHandler("onClientGUIClick",windowbutton1,myFunction,false) I press the button and nothing happens Link to comment
Spakye Posted May 19, 2021 Share Posted May 19, 2021 Do you call the function window() at some point? Link to comment
fairyoggy Posted May 19, 2021 Author Share Posted May 19, 2021 5 minutes ago, Spakye said: Do you call the function window() at some point? Yes of course. How can I open a window differently if I don't call it Maybe I misunderstand something, but I do it like this: function window() window = guiCreateWindow((sW - 406) / 2, (sH - 461) / 2, 455, 465, "Window", false) windowbutton1 = guiCreateButton(330, 432, 118, 22, "butt", false, window) end function start() window() end addCommandHandler ( "w", start ) function myFunction() outputChatBox ("test", 231,217,176,false ) end addCommandHandler("z",myFunction) addEventHandler("onClientGUIClick",windowbutton1,myFunction,false) Link to comment
Spakye Posted May 19, 2021 Share Posted May 19, 2021 (edited) Ok so the problem is the event handler for the button is out of the function, wich means when the ressource start it can't find the button as it was not created yet so it fails. I recommend you to do something like this : function createGuis() --in this function you place all the guis and event handler attached to guis element window = guiCreateWindow((sW - 406) / 2, (sH - 461) / 2, 455, 465, "Window", false) windowbutton1 = guiCreateButton(330, 432, 118, 22, "butt", false, window) addEventHandler("onClientGUIClick",windowbutton1,myFunction,false) guiSetVisible(window, false) -- we hide the window for now, so you can decide when its gonna be visible end function showWindow() guiSetvisible(window, true) end addCommandHandler ( "w", showWindow ) function myFunction() outputChatBox ("test", 231,217,176,false ) end addCommandHandler("z",myFunction) function ressourceStarted() createGuis() end addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), resourceStarted) -- this event is triggered when the resource start client side The guis elements are created when the resource start and we hide them, then you show them when needed with showWindow() Edited May 19, 2021 by Spakye Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now