Randesady

[HELP] How to do action with every array element

Recommended Posts

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 )

 

Share this post


Link to post
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 1

Share this post


Link to post
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

 

Share this post


Link to post

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

 

 

 

  • Thanks 1

Share this post


Link to post

Oh, thanks, it works! xD

  • Like 1

Share this post


Link to post

@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 by Tails
  • Thanks 1

Share this post


Link to post

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.