Randesady Posted March 24, 2018 Share Posted March 24, 2018 I created "RepairMarkers" and when player hits any of them, their car's health must to increase. But I don't know, how to check every marker here: if isElementWithinMarker( getLocalPlayer(), RepairMarker[1] ) then Instead of "RepairMarker[1]" I want to check every marker Source: function CreateMarkers( thePlayer, seat ) if thePlayer == getLocalPlayer() and seat == 0 then BaysideRM = createMarker( -2259.027, 2386.142, 3.897, "cylinder", 3, 233, 96, 21, 100 ) Area69SkyRM = createMarker( 307.876, 2051.747, 16.587, "cylinder", 5, 233, 96, 21, 100 ) RepairMarker = { BaysideRM, Area69SkyRM } end end function RepairVehicle( hitPlayer, matchingDimension ) if hitPlayer == getLocalPlayer() and matchingDimension == true then local theVehicle = getPedOccupiedVehicle( getLocalPlayer() ) local VehicleHealth = getElementHealth( theVehicle ) local RepairHealth = math.floor( VehicleHealth ) function Repair() if isElementWithinMarker( getLocalPlayer(), RepairMarker[1] ) then if RepairHealth < 1000 then RepairHealth = RepairHealth + 1 setElementHealth( theVehicle, RepairHealth ) else fixVehicle( theVehicle ) playSFX( "script", 11, 1, false ) end else killTimer( RepairTimer ) end end if VehicleHealth < 1000 then RepairTimer = setTimer( Repair, 100, 1001 - math.floor( VehicleHealth ) ) end end end function DestroyMarkers( thePlayer, seat ) if thePlayer == getLocalPlayer() and seat == 0 then for i, v in ipairs( RepairMarker ) do destroyElement( RepairMarker[i] ) end end end addEventHandler( "onClientMarkerHit", getRootElement(), RepairVehicle ) addEventHandler( "onClientVehicleEnter", getRootElement(), CreateMarkers ) addEventHandler( "onClientVehicleExit", getRootElement(), DestroyMarkers ) Link to comment
Moderators IIYAMA Posted March 24, 2018 Moderators Share Posted March 24, 2018 function isElementWithinMarkers (element, markers) for i=1, #markers do if isElementWithinMarker(element, markers[i]) then return true end end return false end if isElementWithinMarkers( getLocalPlayer(), RepairMarker ) then Syntax: bool isElementWithinMarkers( element theElement, table markers ) By looping through all markers and check if the element is in one of the markers. If that is true, then break the loop with the return keyword and send true back. Note: (different name) isElementWithinMarker + s 1 Link to comment
Randesady Posted March 24, 2018 Author Share Posted March 24, 2018 14 minutes ago, IIYAMA said: function isElementWithinMarkers (element, markers) for i=1, #markers do if isElementWithinMarker(element, markers[i]) then return true end end return false end if isElementWithinMarkers( getLocalPlayer(), RepairMarker ) then Syntax: bool isElementWithinMarkers( element theElement, table markers ) By looping through all markers and check if the element is in one of the markers. If that is true, then break the loop with the return keyword and send true back. Note: (different name) isElementWithinMarker + s Like that? It doesn't work function Repair() for i = 1, 2 do if isElementWithinMarker( getLocalPlayer(), RepairMarker[i] ) then if RepairHealth < 1000 then RepairHealth = RepairHealth + 1 setElementHealth( theVehicle, RepairHealth ) else fixVehicle( theVehicle ) playSFX( "script", 11, 1, false ) end return true else killTimer( RepairTimer ) return true end return false end end Link to comment
Moderators IIYAMA Posted March 24, 2018 Moderators Share Posted March 24, 2018 You do not have to write the loop yourself. Add the custom function I wrote for you on top of your script. function isElementWithinMarkers (element, markers) for i=1, #markers do if isElementWithinMarker(element, markers[i]) then return true end end return false end And just apply this part: function RepairVehicle( hitPlayer, matchingDimension ) if hitPlayer == getLocalPlayer() and matchingDimension == true then local theVehicle = getPedOccupiedVehicle( getLocalPlayer() ) local VehicleHealth = getElementHealth( theVehicle ) local RepairHealth = math.floor( VehicleHealth ) function Repair() --if isElementWithinMarker( getLocalPlayer(), RepairMarker[1] ) then -- old -- >>>>>>>>>>>>>> if isElementWithinMarkers( getLocalPlayer(), RepairMarker ) then -- new -- <<<<<<<<<<<<<< if RepairHealth < 1000 then RepairHealth = RepairHealth + 1 setElementHealth( theVehicle, RepairHealth ) else fixVehicle( theVehicle ) playSFX( "script", 11, 1, false ) end else killTimer( RepairTimer ) end end if VehicleHealth < 1000 then RepairTimer = setTimer( Repair, 100, 1001 - math.floor( VehicleHealth ) ) end end end 1 Link to comment
Randesady Posted March 25, 2018 Author Share Posted March 25, 2018 Oh, thanks, it works! 1 Link to comment
Tails Posted March 25, 2018 Share Posted March 25, 2018 (edited) @Randesady I know you fixed it already but your example works if you remove the return false statement and the return true in the else block. When you return something within a loop you'll also break the loop so that's why it never goes past the 1st iteration. Edited March 25, 2018 by Tails 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