..:D&G:.. Posted December 28, 2015 Share Posted December 28, 2015 Hello guys, I'm trying to make a mechanic job where the car parts are picked up by the player. When the mechanic enters a marker, the part should attach to the player, and if he enters again and already has a car part attached to them, the object should get destroyed, but it doesn't really work... The object is attached to the player, but it doesn't get destroyed due to the elementData... parts = { [1015] = { } } function attachCarPart(hitElement, partID) if (tonumber(getElementData(hitElement, "partInHand")) > 0) then local IDpart = tonumber(getElementData(hitElement, "partInHand")) destroyElement(parts[IDpart][hitElement]) parts[IDpart][hitElement] = nil setElementData(hitElement, "partInHand", 0) else parts[partID][hitElement] = part part = createObject(partID, 0, 0, 0) setElementData(hitElement, "partInHand", partID) end end Link to comment
AMARANT Posted December 28, 2015 Share Posted December 28, 2015 That is because your value in the table is nil. You set nil to your table 'parts' and then you're trying to destroy it. Check which values return your variables and use debugscript. Link to comment
..:D&G:.. Posted December 28, 2015 Author Share Posted December 28, 2015 Not really because all that is triggered here: function onMechanicHitMarker( hitElement, piesaID ) attachCarPart(hitElement, 1081) end addEventHandler( "onMarkerHit", mechanicMarker, onMechanicHitMarker ) Link to comment
AMARANT Posted December 28, 2015 Share Posted December 28, 2015 parts = { [1015] = { } } function attachCarPart(hitElement, partID) if (tonumber(getElementData(hitElement, "partInHand")) > 0) then local IDpart = tonumber(getElementData(hitElement, "partInHand")) destroyElement(parts[IDpart][hitElement]) parts[IDpart][hitElement] = nil setElementData(hitElement, "partInHand", 0) else part = createObject(partID, 0, 0, 0) parts[partID][hitElement] = part setElementData(hitElement, "partInHand", partID) end end Link to comment
..:D&G:.. Posted December 28, 2015 Author Share Posted December 28, 2015 Can you tell me what you've changed? Link to comment
AMARANT Posted December 28, 2015 Share Posted December 28, 2015 See lines 13 and 14 in my code and in yours. Link to comment
..:D&G:.. Posted December 28, 2015 Author Share Posted December 28, 2015 So it was all just the order of the code....? -.- Link to comment
AMARANT Posted December 28, 2015 Share Posted December 28, 2015 That's not just an order. That's a logical order. Like I said you set an empty variable to your table and only after that you defined it. 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