Jump to content

[BEGINNER] MTA question


Recommended Posts

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

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

 

  • Thanks 1
Link to comment
  • Moderators
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 by LeonardoWilliams
Link to comment
  • Moderators
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)
  • Thanks 1
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...