Protagonist95 Posted December 13, 2016 Share Posted December 13, 2016 Is it possible to make this code simplyer? function onClientColHit(theElement, matchingDimension) local col = source if ( theElement == getLocalPlayer()) then -- if player hit colshape addEventHandler( "onClientKey", getRootElement(), function(button,press) if (button == "mouse3") then setElementData(getLocalPlayer(),"item",getElementData(getElementAttachedTo(col),"item")) -- set elementData of and object in colshape end end) end end addEventHandler("onClientColShapeHit",getRootElement(),onClientColHit) Link to comment
tosfera Posted December 13, 2016 Share Posted December 13, 2016 There is a way to make it cleaner, that is for sure. Try something like this; local tmpColshape = nil; addEventHandler ( "onClientColShapeHit", getRootElement(), function ( theElement ) if ( theElement == getlocalPlayer() ) then tmpColshape = source; addEventHandler ( "onClientKey", getRootElement(), setItemData ); end end ); addEventHandler ( "onClientColShapeLeave", getRootElement(), function ( theElement ) if ( theElement == getlocalPlayer() ) then tmpColshape = nil; removeEventHandler ( "onClientKey", getRootElement(), setItemData ); end end ); function setItemData ( button, pressed ) if ( button == "mouse3" and pressed ) then local tmpObject = getElementAttachtedTo ( tmpColshape ); setElementData ( getLocalPlayer(), "item", getElementData ( tmpObject, "item" ) ); removeEventHandler ( "onClientKey", getRootElement(), setItemData ); end end Maybe it's also smart to see if the colshape has an object attached to it in the onClientColShapeHit already. To avoid warnings and such. Link to comment
Protagonist95 Posted December 13, 2016 Author Share Posted December 13, 2016 39 minutes ago, tosfera said: There is a way to make it cleaner, that is for sure. Try something like this; local tmpColshape = nil; addEventHandler ( "onClientColShapeHit", getRootElement(), function ( theElement ) if ( theElement == getlocalPlayer() ) then tmpColshape = source; addEventHandler ( "onClientKey", getRootElement(), setItemData ); end end ); addEventHandler ( "onClientColShapeLeave", getRootElement(), function ( theElement ) if ( theElement == getlocalPlayer() ) then tmpColshape = nil; removeEventHandler ( "onClientKey", getRootElement(), setItemData ); end end ); function setItemData ( button, pressed ) if ( button == "mouse3" and pressed ) then local tmpObject = getElementAttachtedTo ( tmpColshape ); setElementData ( getLocalPlayer(), "item", getElementData ( tmpObject, "item" ) ); removeEventHandler ( "onClientKey", getRootElement(), setItemData ); end end Maybe it's also smart to see if the colshape has an object attached to it in the onClientColShapeHit already. To avoid warnings and such. Not working.Error in getLocalPlayer lines -- attempt to call global Link to comment
tosfera Posted December 13, 2016 Share Posted December 13, 2016 3 minutes ago, Protagonist95 said: if ( theElement == getlocalPlayer() ) then I used a small L there, just change it to getLocalPlayer() with a capital L and it should work. Link to comment
Protagonist95 Posted December 13, 2016 Author Share Posted December 13, 2016 18 minutes ago, tosfera said: I used a small L there, just change it to getLocalPlayer() with a capital L and it should work. At 22 line:tmpColshape is nil value. Link to comment
tosfera Posted December 13, 2016 Share Posted December 13, 2016 Only way that it can be nil is when the colshape gets deleted before the onClientKey (which should be onClientClick, my bad) gets called. We're assigning the value to tmpColshape on line 5 already so it shouldn't be nil. What other code can be found in your file? 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