LeonardoWilliams Posted May 10, 2021 Share Posted May 10, 2021 Hello there, i recently got into scripting MTA, i learned a lot of new stuff but i`m stucked at a part where i got some ADMIN ICONS that u click and u go ON DUTY, but the thing is they are made with dxDrawImage and i don`t know what to use to make an event that fires when i click that image. My first post i`m glad i can start learning lua with your help and share with you new stuff. Peace guys! Link to comment
Moderators Patrick Posted May 10, 2021 Moderators Share Posted May 10, 2021 Welcome! You have to use onClientClick event. This event triggers whenever the user clicks his mouse. All you have to do is check player clicking "over" the image, you get the coordinates from parameters. Something like that: local imageX, imageY = 500, 500 local imageWidth, imageHeight = 200, 100 addEventHandler("onClientClick", root, function(button, state, clickX, clickY) if button == "left" and state == "up" then -- clicking with left button AND check only release if isClickingInsideArea(clickX, clickY, imageX, imageY, imageWidth, imageHeight) then -- check is click coordinates inside image's zone? outputChatBox("Clicking inside of zone :)") else outputChatBox("Clicking outside of zone :(") end end end) function isClickingInsideArea(clickX, clickY, boxX, boxY, boxWidth, boxHeight) -- check is 'clickX and clickY' inside the image's zone (boxX, boxY, boxWidth, boxHeight)? -- returns 'true' if inside, false otherwise return (clickX >= boxX and clickX <= boxX + boxWidth ) and (clickY >= boxY and clickY <= boxY + boxHeight ) end 1 Link to comment
LeonardoWilliams Posted May 10, 2021 Author Share Posted May 10, 2021 Thank you so much, i love this forum already. On youtube there aren`t that many tutorials and i`m trying to learn how to script 3-4 hours a day. Now i got a source that will make this process much easier. Appreciate it have a nice day dude! 1 Link to comment
Moderators Patrick Posted May 10, 2021 Moderators Share Posted May 10, 2021 7 minutes ago, LeonardoWilliams said: Thank you so much, i love this forum already. On youtube there aren`t that many tutorials and i`m trying to learn how to script 3-4 hours a day. Now i got a source that will make this process much easier. Appreciate it have a nice day dude! Here are some excellent resources to learn the basics of Lua scripting: - https://wiki.multitheftauto.com/wiki/Main_Page - https://wiki.multitheftauto.com/wiki/Scripting_Introduction - https://www.lua.org/manual/5.1/ - https://wiki.multitheftauto.com/wiki/Category:Tutorials - https://forum.multitheftauto.com/topic/34453-manuals - https://forum.multitheftauto.com/topic/64228-the-ultimate-lua-tutorial/ - https://forum.multitheftauto.com/topic/121619-lua-for-absolute-beginners - https://forum.multitheftauto.com/topic/95654-tut-debugging/ - https://forum.multitheftauto.com/topic/114541-tut-events/ - https://forum.multitheftauto.com/topic/117472-tut-scaling-dx/ - https://forum.multitheftauto.com/topic/130181-lua-tables-as-sets-instead-of-arrays/ Videos: https://www.youtube.com/watch?v=Goqj5knKLQM&list=PLY2OlbN3OoCgTGO7kzQfIKPj4tJbYOgSR 1 Link to comment
LeonardoWilliams Posted May 10, 2021 Author Share Posted May 10, 2021 Thank you so much, i`ll start watching right now made my day Link to comment
LeonardoWilliams Posted May 10, 2021 Author Share Posted May 10, 2021 (edited) local adminduty = getElementData(localPlayer, 'duty_admin') addEventHandler("onClientClick", root, function(button, state, clickX, clickY) if button == "left" and state == "up" then -- clicking with left button AND check only release if isClickingInsideArea(clickX, clickY, ax+5,ay+500,32,32) and exports.integration:isPlayerStaff(localPlayer) and adminduty == 0 then -- check is click coordinates inside image's zone? triggerEvent("accounts:settings:updateAccountSettings", localPlayer, "duty_admin", 1) setElementData(localPlayer, 'duty_admin', 1) elseif adminduty == 1 then triggerEvent('accounts:settings:updateAccountSettings', localPlayer, 'duty_admin', 0) setElementData(localPlayer, 'duty_admin', 0) end end end) So i did this, but when i click the second time it doesn t happen anything. If i`m off duty then it just puts me on duty, if i press again nothing happens. Can u please help me or correct my script ? i feel like i need to use a return or something but idk how exactly. @Patrick Edited May 10, 2021 by LeonardoWilliams Link to comment
Moderators Patrick Posted May 10, 2021 Moderators Share Posted May 10, 2021 6 minutes ago, LeonardoWilliams said: So i did this, but when i click the second time it doesn t happen anything. If i`m off duty then it just puts me on duty, if i press again nothing happens. Can u please help me or correct my script ? i feel like i need to use a return or something but idk how exactly. @Patrick I think adminduty variable doesn't defined. Probably you wanted to use getElementData in IF statements. addEventHandler("onClientClick", root, function(button, state, clickX, clickY) if button == "left" and state == "up" then -- clicking with left button AND check only release if isClickingInsideArea(clickX, clickY, ax+5,ay+500,32,32) and exports.integration:isPlayerStaff(localPlayer) then -- check is click coordinates inside images zone? | AND player is staff if getElementData(localPlayer, 'duty_admin') == 0 then triggerEvent("accounts:settings:updateAccountSettings", localPlayer, "duty_admin", 1) setElementData(localPlayer, 'duty_admin', 1) elseif getElementData(localPlayer, 'duty_admin') == 1 then triggerEvent('accounts:settings:updateAccountSettings', localPlayer, 'duty_admin', 0) setElementData(localPlayer, 'duty_admin', 0) end end end end) 1 Link to comment
LeonardoWilliams Posted May 10, 2021 Author Share Posted May 10, 2021 Thank you so much !! It works like charm, you are a saviour ! Link to comment
Moderators Patrick Posted May 10, 2021 Moderators Share Posted May 10, 2021 Just now, LeonardoWilliams said: Thank you so much !! It works like charm, you are a saviour ! 1 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