Volltron Posted June 7, 2020 Share Posted June 7, 2020 (edited) Im struggling with how to use the values without declaring them local outside the function function createTreasure() local treasureRadar = createRadarArea( treasurePositions[randomRadar][1] - 25, treasurePositions[randomRadar][2] - 25, 50, 50, 255, 221, 0, 100) local treasureArea = createColRectangle( treasurePositions[randomRadar][1] - 25, treasurePositions[randomRadar][2] - 25, 50, 50) local treasureObject = createObject(2060, treasurePositions[randomRadar].tabel[randomTreasure][1], treasurePositions[randomRadar].tabel[randomTreasure][2], treasurePositions[randomRadar].tabel[randomTreasure][3] - 1) local treasureCol = createColSphere(treasurePositions[randomRadar].tabel[randomTreasure][1], treasurePositions[randomRadar].tabel[randomTreasure][2], treasurePositions[randomRadar].tabel[randomTreasure][3] - 1, 1.5) attachElements(treasureCol, treasureObject) if (treasureObject) then setObjectScale (treasureObject, 2) end end addEventHandler("onResourceStart", getRootElement(), createTreasure) addEventHandler("onColShapeHit", treasureArea, function(thePlayer) if (getElementType(thePlayer) == "player") then setRadarAreaFlashing( treasureRadar, true ) end end ) addEventHandler("onColShapeLeave", treasureArea, function(thePlayer) if (getElementType(thePlayer) == "player") then setRadarAreaFlashing( treasureRadar, false ) end end ) addEventHandler("onColShapeHit", treasureCol, function(thePlayer) local detect = isElementWithinColShape (thePlayer, treasureCol) if detect then outputChatBox("Write /dig to dig", thePlayer, 255,255,255 ) end end ) function playerDigTreasure(thePlayer, command) local detect = isElementWithinColShape (thePlayer, treasureCol) if detect then setPedAnimation(thePlayer, "BOMBER", "BOM_Plant", -1, false, false, nil, false); setTimer( function() givePlayerMoney( thePlayer, 100000) destroyElement(treasureObject) destroyElement(treasureCol) destroyElement(treasureArea) destroyElement(treasureRadar) outputChatBox("Next treasure spawning in 5 seconds", getRootElement(), 255,255,255 ) setTimer(createTreasure, 5000, 1) end, 2000, 1) end end addCommandHandler("dig", playerDigTreasure) Edited June 7, 2020 by Volltron Link to comment
Scripting Moderators ds1-e Posted June 7, 2020 Scripting Moderators Share Posted June 7, 2020 (edited) 13 minutes ago, Volltron said: Im struggling with how to use the values without declaring them local outside the function function createTreasure() local treasureRadar = createRadarArea( treasurePositions[randomRadar][1] - 25, treasurePositions[randomRadar][2] - 25, 50, 50, 255, 221, 0, 100) local treasureArea = createColRectangle( treasurePositions[randomRadar][1] - 25, treasurePositions[randomRadar][2] - 25, 50, 50) local treasureObject = createObject(2060, treasurePositions[randomRadar].tabel[randomTreasure][1], treasurePositions[randomRadar].tabel[randomTreasure][2], treasurePositions[randomRadar].tabel[randomTreasure][3] - 1) local treasureCol = createColSphere(treasurePositions[randomRadar].tabel[randomTreasure][1], treasurePositions[randomRadar].tabel[randomTreasure][2], treasurePositions[randomRadar].tabel[randomTreasure][3] - 1, 1.5) attachElements(treasureCol, treasureObject) if (treasureObject) then setObjectScale (treasureObject, 2) end end addEventHandler("onResourceStart", getRootElement(), createTreasure) addEventHandler("onColShapeHit", treasureArea, function(thePlayer) if (getElementType(thePlayer) == "player") then setRadarAreaFlashing( treasureRadar, true ) end end ) addEventHandler("onColShapeLeave", treasureArea, function(thePlayer) if (getElementType(thePlayer) == "player") then setRadarAreaFlashing( treasureRadar, false ) end end ) addEventHandler("onColShapeHit", treasureCol, function(thePlayer) local detect = isElementWithinColShape (thePlayer, treasureCol) if detect then outputChatBox("Write /dig to dig", thePlayer, 255,255,255 ) end end ) function playerDigTreasure(thePlayer, command) local detect = isElementWithinColShape (thePlayer, treasureCol) if detect then setPedAnimation(thePlayer, "BOMBER", "BOM_Plant", -1, false, false, nil, false); setTimer( function() givePlayerMoney( thePlayer, 100000) destroyElement(treasureObject) destroyElement(treasureCol) destroyElement(treasureArea) destroyElement(treasureRadar) outputChatBox("Next treasure spawning in 5 seconds", getRootElement(), 255,255,255 ) setTimer(createTreasure, 5000, 1) end, 2000, 1) end end addCommandHandler("dig", playerDigTreasure) You should use locals whenever you can. local col1 = createColSphere(...) local col2 = createColSphere(...) local col3 = createColSphere(...) do col1 = "still local, but it's a string now" col2 = "still local, but it's a string now" col3 = "still local, but it's a string now" end Tip: Attaching resource start event to root will cause that function will be triggered when ANY resource starts, use resourceRoot instead. addEventHandler("onResourceStart", getRootElement(), createTreasure) Edited June 7, 2020 by majqq 1 Link to comment
MrKAREEM Posted June 7, 2020 Share Posted June 7, 2020 (edited) function createTreasure() treasureRadar = createRadarArea( treasurePositions[randomRadar][1] - 25, treasurePositions[randomRadar][2] - 25, 50, 50, 255, 221, 0, 100) treasureArea = createColRectangle( treasurePositions[randomRadar][1] - 25, treasurePositions[randomRadar][2] - 25, 50, 50) treasureObject = createObject(2060, treasurePositions[randomRadar].tabel[randomTreasure][1], treasurePositions[randomRadar].tabel[randomTreasure][2], treasurePositions[randomRadar].tabel[randomTreasure][3] - 1) treasureCol = createColSphere(treasurePositions[randomRadar].tabel[randomTreasure][1], treasurePositions[randomRadar].tabel[randomTreasure][2], treasurePositions[randomRadar].tabel[randomTreasure][3] - 1, 1.5) attachElements(treasureCol, treasureObject) if (treasureObject) then setObjectScale (treasureObject, 2) end end addEventHandler("onResourceStart", getRootElement(), createTreasure) addEventHandler("onColShapeHit", treasureArea, function(thePlayer) if (getElementType(thePlayer) == "player") then setRadarAreaFlashing( treasureRadar, true ) end end ) addEventHandler("onColShapeLeave", treasureArea, function(thePlayer) if (getElementType(thePlayer) == "player") then setRadarAreaFlashing( treasureRadar, false ) end end ) addEventHandler("onColShapeHit", treasureCol, function(thePlayer) local detect = isElementWithinColShape (thePlayer, treasureCol) if detect then outputChatBox("Write /dig to dig", thePlayer, 255,255,255 ) end end ) function playerDigTreasure(thePlayer, command) local detect = isElementWithinColShape (thePlayer, treasureCol) if detect then setPedAnimation(thePlayer, "BOMBER", "BOM_Plant", -1, false, false, nil, false); setTimer( function() givePlayerMoney( thePlayer, 100000) destroyElement(treasureObject) destroyElement(treasureCol) destroyElement(treasureArea) destroyElement(treasureRadar) outputChatBox("Next treasure spawning in 5 seconds", getRootElement(), 255,255,255 ) setTimer(createTreasure, 5000, 1) end, 2000, 1) end end addCommandHandler("dig", playerDigTreasure) delete the locals and the variables will read in the whole events and it will be better if you check the elements before destroy it or put it in events using IsElement + the point that majqq said about resource event Edited June 7, 2020 by MrKAREEM 1 Link to comment
Volltron Posted June 7, 2020 Author Share Posted June 7, 2020 25 minutes ago, MrKAREEM said: function createTreasure() treasureRadar = createRadarArea( treasurePositions[randomRadar][1] - 25, treasurePositions[randomRadar][2] - 25, 50, 50, 255, 221, 0, 100) treasureArea = createColRectangle( treasurePositions[randomRadar][1] - 25, treasurePositions[randomRadar][2] - 25, 50, 50) treasureObject = createObject(2060, treasurePositions[randomRadar].tabel[randomTreasure][1], treasurePositions[randomRadar].tabel[randomTreasure][2], treasurePositions[randomRadar].tabel[randomTreasure][3] - 1) treasureCol = createColSphere(treasurePositions[randomRadar].tabel[randomTreasure][1], treasurePositions[randomRadar].tabel[randomTreasure][2], treasurePositions[randomRadar].tabel[randomTreasure][3] - 1, 1.5) attachElements(treasureCol, treasureObject) if (treasureObject) then setObjectScale (treasureObject, 2) end end addEventHandler("onResourceStart", getRootElement(), createTreasure) addEventHandler("onColShapeHit", treasureArea, function(thePlayer) if (getElementType(thePlayer) == "player") then setRadarAreaFlashing( treasureRadar, true ) end end ) addEventHandler("onColShapeLeave", treasureArea, function(thePlayer) if (getElementType(thePlayer) == "player") then setRadarAreaFlashing( treasureRadar, false ) end end ) addEventHandler("onColShapeHit", treasureCol, function(thePlayer) local detect = isElementWithinColShape (thePlayer, treasureCol) if detect then outputChatBox("Write /dig to dig", thePlayer, 255,255,255 ) end end ) function playerDigTreasure(thePlayer, command) local detect = isElementWithinColShape (thePlayer, treasureCol) if detect then setPedAnimation(thePlayer, "BOMBER", "BOM_Plant", -1, false, false, nil, false); setTimer( function() givePlayerMoney( thePlayer, 100000) destroyElement(treasureObject) destroyElement(treasureCol) destroyElement(treasureArea) destroyElement(treasureRadar) outputChatBox("Next treasure spawning in 5 seconds", getRootElement(), 255,255,255 ) setTimer(createTreasure, 5000, 1) end, 2000, 1) end end addCommandHandler("dig", playerDigTreasure) delete the locals and the variables will read in the whole events and it will be better if you check the elements before destroy it or put it in events using IsElement + the point that majqq said about resource event I did it and now it works, thanks for help ^^ I also had to put the event handlers for colshapes inside the create function to make it work 1 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