KaMi Posted July 20, 2018 Posted July 20, 2018 (edited) Hello ! I recently made a weapon that shoots explosives. I thought about adding an object in the form of decoration using the bone attach resource. Everything go well until I realized that , when the player dies, the object stayed stuck and was not destroyed. Here you have the code function activArmas( player ) if (getElementData (player, "Armactivada") == false) then setElementData (player, "Armactivada", true) outputChatBox( "Activaste el arma", player, 100, 0, 0 ) else setElementData (player, "Armactivada", false) outputChatBox( "Desactivaste el arma", player, 100, 0, 0 ) end end addCommandHandler("activar", activArmas) local frascote = {} function setObjectInWeapon( previousWeaponID, currentWeaponID ) if (getElementData (source, "Armactivada") == true) then if ( currentWeaponID == 31 ) then exports.bone_attach:attachElementToBone(frascote[source],source,12,-0.09,0,-0.03,0,2,40) elseif ( previousWeaponID == 31 ) then exports.bone_attach:detachElementFromBone( frascote[source] ) destroyElement( frascote[source] ) end end end addEventHandler("onPlayerWeaponSwitch", root, setObjectInWeapon) function wast() if (getElementData (source, "Armactivada") == true) then destroyElement( frascote[source] ) --Here i have my problem. end end addEventHandler("onPlayerWasted", root, wast) Sorry for repost the topic but the spanish community is not than active . Thanks a lot ! Edited July 20, 2018 by CodyJ(L) Fixed lua format
KaMi Posted July 20, 2018 Author Posted July 20, 2018 5 minutes ago, CodyJ(L) said: Moved to scripting, anywho; use tables. thanks, can you pls give an example ? my mind is blank
KaMi Posted July 20, 2018 Author Posted July 20, 2018 (edited) 10 minutes ago, CodyJ(L) said: Where is frascote being created? i forget specify this frascote[source] = frasco answering your question... i think in the line 13. I don't usually use tables Edited July 20, 2018 by <~KaMiKaZe~>
Captain Cody Posted July 20, 2018 Posted July 20, 2018 Post the code where it's created. Not just the line
좋은n4ZON Posted July 20, 2018 Posted July 20, 2018 if not objectTable then objectTable = {}; end objectTable[playerSource] = createObject(id, x,y ,z) -- write there the attach -- and detach if isElement(objectTable[playerSource]) then detachElements(objectTable[playerSource]) destroyElement(objectTable[playerSource]) end
KaMi Posted July 20, 2018 Author Posted July 20, 2018 (edited) 7 hours ago, Sanyika666 said: if not objectTable then objectTable = {}; end objectTable[playerSource] = createObject(id, x,y ,z) -- write there the attach -- and detach if isElement(objectTable[playerSource]) then detachElements(objectTable[playerSource]) destroyElement(objectTable[playerSource]) end I put the code in this form, but nothing if not objectTable then objectTable = {}; end objectTable[playerSource] = createObject(2976,0,0,0) function setObjectInWeapon( previousWeaponID, currentWeaponID ) if (getElementData (source, "Armactivada") == true) then if ( currentWeaponID == 31 ) then exports.bone_attach:attachElementToBone(objectTable[playerSource],source,12,-0.09,0,-0.03,0,2,40) elseif ( previousWeaponID == 31 ) then exports.bone_attach:detachElementFromBone( objectTable[playerSource] ) destroyElement( objectTable[playerSource] ) end end end addEventHandler("onPlayerWeaponSwitch", root, setObjectInWeapon) function wast() if (getElementData (source, "Armactivada") == true) then destroyElement( objectTable[playerSource] ) end end addEventHandler("onPlayerWasted", root, wast) if isElement(objectTable[playerSource]) then detachElements(objectTable[playerSource]) destroyElement(objectTable[playerSource]) end the console give me this error : ERROR: Weap\server.lua:24 table index is nil 7 hours ago, CodyJ(L) said: Post the code where it's created. Not just the line SERVER-SIDE if not objectTable then objectTable = {}; end objectTable[playerSource] = createObject(2976,0,0,0) function setObjectInWeapon( previousWeaponID, currentWeaponID ) if (getElementData (source, "Armactivada") == true) then if ( currentWeaponID == 31 ) then exports.bone_attach:attachElementToBone(objectTable[playerSource],source,12,-0.09,0,-0.03,0,2,40) elseif ( previousWeaponID == 31 ) then exports.bone_attach:detachElementFromBone( objectTable[playerSource] ) destroyElement( objectTable[playerSource] ) end end end addEventHandler("onPlayerWeaponSwitch", root, setObjectInWeapon) function wast() if (getElementData (source, "Armactivada") == true) then destroyElement( objectTable[playerSource] ) end end addEventHandler("onPlayerWasted", root, wast) if isElement(objectTable[playerSource]) then detachElements(objectTable[playerSource]) destroyElement(objectTable[playerSource]) end CLIENT-SIDE function fireGG_135( weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement, startX, startY, startZ ) if (getElementData (source, "Armactivada") == true) then if ( weapon == 31 ) then local genergy = createMarker( 0, 0, 0, "corona", 1, 0, 165, 57, 255 ) local genergyo = createObject( 2976, startX, startY, startZ ) setElementAlpha( genergyo, 0 ) attachElements( genergy, genergyo, 0, 0, 0 ) moveObject( genergyo, 1000, hitX, hitY, hitZ ) setElementCollisionsEnabled( genergyo, false ) setTimer( function() destroyElement( genergyo ) destroyElement( genergy ) createExplosion( hitX, hitY, hitZ, 2 ) end, 1500, 1 ) end end end addEventHandler("onClientPlayerWeaponFire", root, fireGG_135) Edited July 20, 2018 by <~KaMiKaZe~>
Captain Cody Posted July 20, 2018 Posted July 20, 2018 objectTable[playerSource] = createObject(2976,0,0,0) There's your problem.
KaMi Posted July 20, 2018 Author Posted July 20, 2018 2 minutes ago, CodyJ(L) said: objectTable[playerSource] = createObject(2976,0,0,0) There's your problem. Ho, what i have to do here ?
Captain Cody Posted July 20, 2018 Posted July 20, 2018 That's just thrown in there, (PlayerSource) doesn't even exist.
KaMi Posted July 20, 2018 Author Posted July 20, 2018 9 minutes ago, CodyJ(L) said: That's just thrown in there, (PlayerSource) doesn't even exist. I'm sorry for bothering but I'm very confused. What can I do?
Bossy Posted July 26, 2018 Posted July 26, 2018 Firstly I recommend using attachElements which is optimal, so you could use this function detachElements and then destroyElement so that it does not stay in the player and if that doesn't work; instead use bone attach, which uses best attach element is more easy
Recommended Posts