Jump to content

[help] clickedelement


Recommended Posts

function PegarVaso( source )
		local Px, Py, Pz = getElementPosition( source )
		Vaso = createObject( 2203, Px, Py, Pz )
		setPedAnimation( source, "CARRY", "crry_prtial", 4.1, true, true, true )
		exports.pAttach:attach(Vaso, source, 1.7, 0, 0.4, 0.4, 90, 0, 0 )
end
 addCommandHandler( "pegarvaso", PegarVaso )


function LargarVaso( source )
			local Px, Py, Pz = getElementPosition( source )
			setPedAnimation( source, "CARRY", "putdwn", 1.0, false, false, false, true ) 
		setTimer(function()
			setPedAnimation( source )
			destroyElement( Vaso )
			Planta = createObject( 2203, Px , Py , Pz -0.85)
			setElementData( Planta, "Planta", true )
		end, 1200, 1) 
end
 addCommandHandler( "soltarvaso", LargarVaso )

function Planta( button, state, player ) -- Add the function
    if button == "left" and state == "down" then
        if getElementData(Planta, "Planta") == true then
				triggerClientEvent( "MenuPlantar", player )
        end
    end
end
addEventHandler( "onElementClicked", root, Planta )

 

 

i trying to use elementdata for recognize the object, but the client event is triggered clicking in every element, how i can properly make the function recognize the object element data?

Link to comment
  • Moderators

Hi.

When you check the element-data, here 'if getElementData(Planta, "Planta") == true then', you have to use source instead of Planta.
On wiki, you can see source is the element that got clicked by the player.

Because now, you only always check the last created object's element-data.
Always make your variables local otherwise they become global variables, and most of times you don't want that... mostly on server-side.

In your case, I see why you didn't make it local, but it's a bad practise. You can't store multiple players' carried objects in one single variable... so you have to store them in a table.

Something like that:

local objectCarries = {} -- store carryed objects in this table

function PegarVaso( player )
    local Px, Py, Pz = getElementPosition( player )
    local Vaso = createObject( 2203, Px, Py, Pz )
  	objectCarries[player] = Vaso -- store: player carry this object
    setPedAnimation( player, "CARRY", "crry_prtial", 4.1, true, true, true )
    exports.pAttach:attach(Vaso, player, 1.7, 0, 0.4, 0.4, 90, 0, 0 )
end
addCommandHandler( "pegarvaso", PegarVaso )

function LargarVaso( player )
    local Px, Py, Pz = getElementPosition( player )
    setPedAnimation( player, "CARRY", "putdwn", 1.0, false, false, false, true ) 
    
    setTimer(function()
        setPedAnimation( player )
      	
      	if objectCarries[player] and isElement(objectCarries[player]) then -- is player carrying an object?
        	destroyElement( objectCarries[player] ) -- yes... so destroy it
        	objectCarries[player] = nil -- and don't forget to remove it from our table too
        end
      
        local Planta = createObject( 2203, Px , Py , Pz -0.85)
        setElementData( Planta, "Planta", true )
    end, 1200, 1) 
end
addCommandHandler( "soltarvaso", LargarVaso )

function Planta( button, state, player ) -- Add the function
    if button == "left" and state == "down" then
        if getElementData(source, "Planta") == true then
            triggerClientEvent( "MenuPlantar", player )
        end
    end
end
addEventHandler( "onElementClicked", root, Planta )

 

... and one more thing, source is a hidden variable what MTA define for you in most of times. Don't call your variables as source because you overwrite the hidden one... and can cause other problems.

Edited by Patrick
  • 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...