Administrators Lpsd Posted January 2, 2017 Administrators Share Posted January 2, 2017 (edited) I'm trying to create a custom pickup which gets destroyed when a player touches it, and respawns after 5 seconds I've created the custom pickup using "createPickup", and also placed a colSphere (using "createColSphere") at the same position. When the player hits the pickup for the first time, it disappears and respawns successfully 5 seconds later. However, after it has respawned once, it won't destroy again when you touch it. My code is below. local x = 3985.1799316406 local y = -1973.4870605469 local z = 27.812973022461 pickup = createPickup(x, y, z, 3, 1242) -- create custom pickup pickupCol = createColSphere ( x, y, z, 1) -- create colSphere for pickup function removePickup() destroyElement(pickup) -- destroy pickup destroyElement(pickupCol) -- destroy pickup colsphere setTimer(function() pickup = createPickup(x, y, z, 3, 1242) pickupCol = createColSphere ( x, y, z, 1) end, 5000, 1) -- spawn new pickup and colsphere after 5 seconds end addEventHandler ( "onColShapeHit", pickupCol, removePickup ) Any thoughts? edit: I did try using the respawnTime argument on createPickup, for 5 seconds, but it didn't have any effect, which is why I'm trying to do it this way. No errors returning either. Edited January 2, 2017 by LopSided_ Link to comment
Miika Posted January 3, 2017 Share Posted January 3, 2017 (edited) local x = 3985.1799316406 local y = -1973.4870605469 local z = 27.812973022461 function addPickup() pickup = createPickup(x, y, z, 3, 1242) end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), addPickup) pickupCol = createColSphere ( x, y, z, 1) -- create colSphere for pickup addEventHandler("onColShapeHit", root, function(hitElement) if source == pickupCol then if getElementType(hitElement) == "Player" then if isElement(pickup) then destroyElement(pickup) setTimer(function() addPickup() end, 5000, 1) end end end end ) Edited January 3, 2017 by Miika822 Link to comment
Administrators Lpsd Posted January 3, 2017 Author Administrators Share Posted January 3, 2017 Didn't work, however I solved it myself using the original code I posted. Stopped destroying the colSphere element, onColShapeHit Set 'pickup' variable to nil after destroying it with destroyElement Added a check to see if "pickup" exists onColShapeHit destroyElement doesn't "destroy" the variable, just the element in the game, so you have to reset it. local x = 3985.1799316406 local y = -1973.4870605469 local z = 27.812973022461 pickup = createPickup(x, y, z, 3, 1242) -- create custom pickup pickupCol = createColSphere ( x, y, z, 1) -- create colSphere for pickup function removePickup() if (pickup) then destroyElement(pickup) -- destroy pickup pickup = nil setTimer(function() pickup = createPickup(x, y, z, 3, 1242) end, 5000, 1) -- spawn new pickup and colsphere after 5 seconds end end addEventHandler ( "onColShapeHit", pickupCol, removePickup ) 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