Jump to content

[HELP] onClientClick [Solved]


UnG//Macaus

Recommended Posts

I have a problem with my code, everything is working, but the function is executed 2 times when I click on a button. Can someone help me?

    for id,element_r in ipairs(getElementChildren(dxRootElement)) do 
        for id, element in ipairs(getElementChildren(element_r)) do 
            if getElementData(element,"visible") == true then 
                local g_height = getElementData(element,"height") or 0 
                local g_width = getElementData(element,"width") or 0 
                local px, py = dxGetPosition(element) 
                if (button == "left" and state == "up") then 
                    if x >= px and x <= (px+g_width) and y >= py and y <= (py + g_height) then 
                        triggerEvent("onClientDXClick",element) 
                    end 
                end 
            end 
        end 
    end 

Edited by Guest
Link to comment

That's because your for-loop executes more than one time before you release/press your key. Just break the loop using:

break 

Example: ( Tho i don't know how your script works as you are not showing it entierly ):

triggerEvent("onClientDXClick",element) 
break 

That will prevent your for-loop to continue looping after you triggered the event

Link to comment

now is not repeating more, except for triggerEvents

Example:

addEventHandler("onClientDXClick",getRootElement(), 
function() 
    if (source == button) then 
        triggerServerEvent("test",localPlayer) 
    end 
end) 
  
--server 
addEvent("test",true) 
addEventHandler("test",getRootElement(), 
function() 
    outputChatBox ( "TEST", source, 0, 255, 0) 
end) 
  

Link to comment

I think you have misunderstood the problem I have now

addEventHandler("onClientDXClick",getRootElement(), 
function() 
    if (source == button) then 
        triggerServerEvent("test",localPlayer) --run 2 times (2 outputChatBox) 
        outputChatBox("test") --run 1 time (1 outputChatBox) 
    end 
end) 

Link to comment

You can use that function :

function isMouseWithinRangeOf(posX, posY, sizeX, sizeY) 
  if isCursorShowing() == false then 
    return false 
  end 
  local cx,cy = getCursorPosition() 
  cx,cy = cx*sW,cy*sH 
  if cx >= posX and cx <= posX+sizeX and cy >= posY and cy <= posY+sizeY then 
    return true,cx,cy 
  else 
    return false 
  end 
end 
  

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