Jump to content

[RESOLVED] "onClientClick" not triggering.


Recommended Posts

Hi :),

So like today i tried to use "onClientClick" clientside event to detect a click on an element (that is an ATM.)

One thing you might want to know, is that the atm is actually created in the MAP EDITOR, which, i think, means it's created server-side, right?

I used search in the wiki and on this forum for the past hour, and didn't come to anything concluding.

I now rely on YOU! :D

Here's my stuff : (CLIENT SIDE CODE)

  
---- Banking System (clientside) 
  
--[[ ATM IDs list 
    LS_bank_ATM 
--]] 
  
local LS_BANK_ATM = getElementByID("LS_bank_ATM") 
setElementData(LS_BANK_ATM, "type", "atm") 
setElementData(LS_BANK_ATM, "name", "LS Bank ATM") 
  
function atmClicked(button, state, _,_, wx, wy, wz, element) 
    if element and getElementData(element, "type") == "atm" and state == "down" then 
        local x, y, z = getElementPosition(localPlayer) 
        if getDistanceBetweenPoints3D(x, y, z, wx, wy, wz) <= 10 then 
            outputChatBox("DEBUGMSG::You clicked the atm.", 0,0,255) 
    end 
    end 
end 
addEventHandler("onClientClick", root, atmClicked) 
  

I took the code solidSnake14 kindly provides here on the forum, and modded it to my usage, but i can't make it work somehow.

Thanks in advance :)

Edited by Guest
Link to comment

The first problem you have is on line 16, the outputChatBox() function, you don't tell it who to output to.

"NOTE: When using optional arguments, you must supply all arguments before the one you wish to use. For more information on optional arguments..."

EDIT: Second argument is who to output to, in your case it'd be:

            outputChatBox("DEBUGMSG::You clicked the atm.", localPlayer, 0,0,255) 

Considering the fact this is the only way you test if they click it, that may be what has deceived you.

Furthermore, consider instead using the function:

https://wiki.multitheftauto.com/wiki/CreateObject

Instead of the map editor, the trouble with what you do now is all objects with name 'LS_Bank_ATM' will do this (probably not a big one, but all the same it's useful to have more granular control. This will allow you to permit certain functions on certain ATMs.

Link to comment

Hey, thanks for stopping by!

Indeed, i don't need to provide the 'target' for outputChatBox, when on clientside, as the target is the localplayer :)

Though i'll still try to create my ATMs directly with createObject as you said, might be why it doesn't detect it!

Link to comment

Do you have one ATM object and it's id is 'LS_bank_ATM'?

If you have more than one, then you have a problem because you're retrieving one element by the ID, and setting it's element data. So the check will work on that ATM object only.

EDIT @ Unique: Won't matter serverside or clientside, you can interact with objects on both sides, either using a .map file or createObject, won't matter. Check what I said above.

Link to comment

try this:

---- Banking System (clientside) 
  
--[[ ATM IDs list 
    LS_bank_ATM 
--]] 
  
local LS_BANK_ATM = getElementByID("LS_bank_ATM") 
setElementData(LS_BANK_ATM, "type", "atm") 
setElementData(LS_BANK_ATM, "name", "LS_Bank_ATM") 
  
function atmClicked(button, state, _,_, wx, wy, wz, element) 
    if button and state == "down" then 
        if(element)then 
            outputChatBox("You've clicked on "..getElementModel(element).."." 
            if(getElementData(element, "type") == "atm")then 
                local x, y, z = getElementPosition(localPlayer)  
                if getDistanceBetweenPoints3D(x, y, z, wx, wy, wz) <= 10 then 
                    outputChatBox("DEBUGMSG::You clicked the atm.", 0,0,255) 
                end 
            end 
        end 
    end 
end 
addEventHandler("onClientClick", root, atmClicked) 

Edited by Guest
Link to comment
Do you have one ATM object and it's id is 'LS_bank_ATM'?

If you have more than one, then you have a problem because you're retrieving one element by the ID, and setting it's element data. So the check will work on that ATM object only.

[...]

Yep, i made sure i have only one ID for one atm, others ATMs are ID'ed differently, to avoid conflicts.

@Jaysds1 : I just tried your version, but sadly it doesn't change the outcome, still trying to figure out, 'cause in my mind it makes sense. xD

Thanks for trying though! :D

Link to comment

Yep i'm totally sure i'm doing it right :D

Sorry for the late answer, there was 8 hours sleep inbetween lol.

Still investigating! :)

EDIT : btw I tried with onElementClick too (which is a serversided event, correct me if i'm wrong), but it does exactly the same result : nothing.

EDIT 2 : Someone told me about "debugscript 3", i'm gonna give it a shot see if there's something i'm missing.

EDIT 3 : Nopes, no typo, nothing, not even a simple warning. Idk!

EDIT 4 : What if i add a colshape around the ATM so it detects a collision, should that work? ALSO, i'm using nightly build from 3 days ago (23 may 2012), maybe the bug has been introduced then?

EDIT 5 : I'll temporarly use markers as a solution, but i'd really like to find how to do this "onClientClick" thing, if anyone has a suggestion, you're more than welcome!

Link to comment
I've edited the script again, try it...

When you click on an element, see if the model in the chat is the same as the model as the atm...

Hey :)

I just tried your new attempt, and actually, no model at all is appearing in the chat box, altough i'm trying to click on the atm from a very close distance.

It's like my click wasn't detected at all, it's really weird...

Link to comment

try adding the script from the wiki and click on random things... here:

local myLabel = guiCreateLabel  ( 0, 0, 1, 1, "", true ) 
  
function addLabelOnClick ( button, state, absoluteX, absoluteY, worldX, worldY, worldZ, clickedElement ) 
        --if an element was clicked on screen 
        if ( clickedElement ) then 
                --retreive the element type 
                local elementType = getElementType ( clickedElement ) 
                --change the label text to that element type 
                guiSetText ( myLabel, elementType ) 
                --and place it in the position of where the element is 
                guiSetPosition ( myLabel, absoluteX, absoluteY, false ) 
                --hide the text by passing an empty string 5 seconds later 
                setTimer ( guiSetText, 5000, 1, myLabel, "" ) 
        end 
end 
addEventHandler ( "onClientClick", getRootElement(), addLabelOnClick ) 

Link to comment

This gets me worried because nothing works, i can click on anything (world object or server created object) nothing works... Now i guess it's not from my side then?

Thanks all for the help!

EDIT : You know what? I'll try to rollback to the last NON-NIGHTLY build of MTA. What version are you guys using? And does "onClientClick" work for you?

EDIT 2 : Wait wait, it worked there, but i had to have a GUI already opened somehow, but i saw a label "VEHICLE" written when i was clicking on a vehicle. But another GUI had to be opened or it didn't work. Also if i substitute the GUI label with outputChatBox it still doesn't work. Weirddd

Link to comment

Man!! I found the answer as i was fiddling around in my code. Then I thought like "if the clicks works with admin panel, then it must be using some kind of different click that i use or something".

In fact, i was making the GUI CURSOR appear from the client side, aka with "guiSetInputEnabled" which is wrong!

I had to make the cursor appear with the serversided function "showCursor"..............

I told you it was retarded lol!

This will teach me for mixing clientside and serverside...

My basic idea was like : ok my gui and my click are client side, so my client will detect the ped and tell me what element it is. BUT NO! Ped is serversided!

PFfffff :P

Thanks all for your collaboration! :)

Marked as resolved.

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