Jump to content

Wierd marker behaviour.


AfuSensi

Recommended Posts

Hi all,

I got two questions about markers.

Q1: When i ride trough a marker, the event 'onMarkerHit' fires twice. I have no idea what's wrong with it. Function that hits twice:

function hittingthemarker ( hitElement ) 
local randomfunctionfromtable = functionstable[math.random(1,#functionstable)] 
    if getElementType( hitElement ) == "player" then 
        if isPlayerInVehicle( hitElement ) then 
                        setTimer (_G[randomfunctionfromtable], 50, 1) 
                        setElementPosition (_G[createdmarker], 0,0,0) 
  
  
        end 
    end 
end  
addEventHandler('onMarkerHit', getRootElement(), hittingthemarker) 

Q2: How can i detect what marker the player drove trough? I am creating the markers via a loop trough the objects, so search for a certain objects for the cordinations.

function checkforheads () -- Checks for heads, replaces it with markers and makes the heads invisible and with no collision -- 
  
    for _, t in pairs( allObjects ) do 
        if ( getElementModel (t) == 2908 ) then 
            x,y,z = getElementPosition (t) -- Gets xyz position from the heads 
            createdmarker = createMarker (x, y, z, "cylinder", markersize, 0, 255, 0, 150) -- Creates the markers 
            setElementAlpha ( t, 0 ) 
            setElementCollisionsEnabled(t, false) 
  
        end 
    end 
end 
addEventHandler ( "onResourceStart", getRootElement(), checkforheads ) 

I want the marker to move if a player drove trough it ( setElementPosition ), but it seems that there is no way to detect what marker the player drove trough, only IF the player drove trough a marker.

If i do this:

function hittingthemarker ( hitElement ) 
local randomfunctionfromtable = functionstable[math.random(1,#functionstable)] 
    if getElementType( hitElement ) == "player" then 
        if isPlayerInVehicle( hitElement ) then 
                        setTimer (_G[randomfunctionfromtable], 50, 1) 
                        setElementPosition (_G[createdmarker], 0,0,0) 
  
  
        end 
    end 
end  
addEventHandler('onMarkerHit', getRootElement(), hittingthemarker) 

It gives an error : 'Bad argument @ 'setElementPosition' [Expected element at argument 1, got nil]'

Here is my full script (server) :

markersize = 2 -- Set Marker Size -- 
respawntime = 10000 -- Set marker respawn time in milliseconds ( 1sec = 1000 ) -- 
  
  
  
  
  
  
functionstable =   {  
"MassiveCar",  
"Barrel",  
"Haybale",  
"Rocket"  
} 
  
  
  
  
  
  
  
allObjects = getElementsByType ( "object" ) 
  
  
function destroyothermarkers () -- Destroys all markers that are not from this script -- 
 previousmarkers = getElementsByType ("marker") 
 for _, prevmark in pairs( previousmarkers ) do 
 destroyElement ( prevmark ) 
end 
end 
  
addEventHandler ( "onResourceStart", getRootElement(), destroyothermarkers ) 
  
  
  
function checkforheads () -- Checks for heads, replaces it with markers and makes the heads invisible and with no collision -- 
  
    for _, t in pairs( allObjects ) do 
        if ( getElementModel (t) == 2908 ) then 
            x,y,z = getElementPosition (t) -- Gets xyz position from the heads 
            createdmarker = createMarker (x, y, z, "cylinder", markersize, 0, 255, 0, 150) -- Creates the markers 
            setElementAlpha ( t, 0 ) 
            setElementCollisionsEnabled(t, false) 
  
        end 
    end 
end 
addEventHandler ( "onResourceStart", getRootElement(), checkforheads ) 
  
  
  
  
  
function MarkerColorChange () -- Random Colors for the markers 
allMarkers = getElementsByType ("marker") 
  
  
for _, mrkr in pairs( allMarkers ) do 
        if ( getMarkerSize (mrkr) == markersize ) then 
            setTimer (function() setMarkerColor ( mrkr, math.random (0,255), math.random (0,255), math.random (0,255), 80 ) end, 150, 0 ) 
             
  
        end 
    end 
end 
  
  
addEventHandler ( "onResourceStart", getRootElement(), MarkerColorChange ) 
  
  
--------------------HIT FUNCTIONS----------------- 
function MassiveCar () 
    outputChatBox ("MassiveCar Function") 
end 
  
  
function Barrel () 
    outputChatBox ("Barrel Function") 
end 
  
  
function Haybale () 
    outputChatBox ("Haybale Function") 
end 
  
  
function Rocket () 
    outputChatBox("Rocket Function") 
end 
  
-------------------END HIT FUNCTIONS------------- 
  
function hittingthemarker ( hitElement ) 
local randomfunctionfromtable = functionstable[math.random(1,#functionstable)] 
    if getElementType( hitElement ) == "player" then 
        if isPlayerInVehicle( hitElement ) then 
                        setTimer (_G[randomfunctionfromtable], 50, 1) 
                        setElementPosition (_G[createdmarker], 0,0,0) 
  
  
        end 
    end 
end  
addEventHandler('onMarkerHit', getRootElement(), hittingthemarker) 

Link to comment
The marker the player drove through is simply the source element of onMarkerHit.. and it gets fired twice because if the player is in a vehicle it triggers for the vehicle and the player separately.

Thanks, i got the source part working, Now the only thing that still needs fixing is to let the even trigger once.

function hittingthemarker ( hitElement ) 
local randomfunctionfromtable = functionstable[math.random(1,#functionstable)] 
    if getElementType( hitElement ) == "player" then 
        if isPlayerInVehicle( hitElement ) then 
                        setTimer (_G[randomfunctionfromtable], 50, 1) 
                        sx,sy,sz = getElementPosition(source) 
                        setElementPosition ( source, 0,0,0) -- makes marker go away -- 
                        disappearedmarker = source -- targets the marker that went away -- 
                        setTimer (function () setElementPosition(disappearedmarker, sx,sy,sz) end, respawntime, 1) -- makes marker come back, set respawn time at top of the script -- 
  
  
        end 
    end 
end  
addEventHandler('onMarkerHit', getRootElement(), hittingthemarker) 

If anyone could guide me on this, it would be greatly appreciated.

Link to comment
  • MTA Team
setElementPosition (_G[createdmarker], 0,0,0) 

You dont have to use _G to access createdmarker there, because you already have the variable

setElementPosition (createdmarker, 0,0,0) 

If i don't, i'll get this error: viewtopic.php?f=91&t=77650

Nonsense. It's not even about the function table. You are assigning a marker element to the global variable, that means you can use directly createdmarker. If you still want to use the _G global table, then use it like that:

_G["createdmarker"] 

Link to comment

the trigger Problem is this ,

if getElementType( hitElement ) == "player" then 
        if isPlayerInVehicle( hitElement ) then 

if you want it trigger when the player in the vehicle , then no need for getElementType = 'player'

remove it , and test it see how it goes ,

Link to comment
setElementPosition (_G[createdmarker], 0,0,0) 

You dont have to use _G to access createdmarker there, because you already have the variable

setElementPosition (createdmarker, 0,0,0) 

If i don't, i'll get this error: viewtopic.php?f=91&t=77650

Nonsense. It's not even about the function table. You are assigning a marker element to the global variable, that means you can use directly createdmarker. If you still want to use the _G global table, then use it like that:

_G["createdmarker"] 

I'm sorry, i got it confused with randomfunctionfromtable. Removed it.

the trigger Problem is this ,

if getElementType( hitElement ) == "player" then 
        if isPlayerInVehicle( hitElement ) then 

if you want it trigger when the player in the vehicle , then no need for getElementType = 'player'

remove it , and test it see how it goes ,

This gives me the error: Bad 'ped' pointer @ 'isPlayerInVehicle' .

function hittingthemarker ( hitElement ) 
local randomfunctionfromtable = functionstable[math.random(1,#functionstable)] 
        if isPlayerInVehicle( hitElement ) then 
                        setTimer (_G[randomfunctionfromtable], 50, 1) 
                        sx,sy,sz = getElementPosition(source) 
                        destroyElement(source) 
  
  
    end 
end  
addEventHandler('onMarkerHit', getRootElement(), hittingthemarker) 

Link to comment

First of All ,

isPlayerInVehicle  

--- has Been Remove ( it's old )

use

isPedInVehicle  

insted

Try this ,

addEventHandler( 'onMarkerHit', resourceRoot, function( hitElement ) 
local randomfunctionfromtable = functionstable[math.random(1,#functionstable)] 
   if ( getElementType( hitElement ) == 'player' ) and not ( isPedInVehicle( hitElement ) ) then 
 setTimer (_G[randomfunctionfromtable], 50, 1) 
 sx,sy,sz = getElementPosition(source) 
destroyElement(source) 
  end 
end 
) 
Link to comment
First of All ,

isPlayerInVehicle  

--- has Been Remove ( it's old )

use

isPedInVehicle  

insted

Try this ,

addEventHandler( 'onMarkerHit', resourceRoot, function( hitElement ) 
local randomfunctionfromtable = functionstable[math.random(1,#functionstable)] 
   if ( getElementType( hitElement ) == 'player' ) and not ( isPedInVehicle( hitElement ) ) then 
 setTimer (_G[randomfunctionfromtable], 50, 1) 
 sx,sy,sz = getElementPosition(source) 
destroyElement(source) 
  end 
end 
) 

This doesn't pass "and not ( isPedInVehicle( hitElement )"

And if i remove "not", it fires up twice like before.

edit: to make things clear, the gamemode is race, so i have to pass the vehicle, but somehow ignore the player.

Link to comment

well if it's a race no need to check if player inside a vehicle becuase he will always be in a vehicle when he player ,

try this ,

addEventHandler( 'onMarkerHit', resourceRoot, function( hitElement ) 
local randomfunctionfromtable = functionstable[math.random(1,#functionstable)] 
   if ( getElementType( hitElement ) == 'vehicle' )  then 
 setTimer (_G[randomfunctionfromtable], 50, 1) 
 sx,sy,sz = getElementPosition(source) 
destroyElement(source) 
  end 
end 
) 

/debugscript 3

tell me the error if there is ,

Link to comment
well if it's a race no need to check if player inside a vehicle becuase he will always be in a vehicle when he player ,

try this ,

addEventHandler( 'onMarkerHit', resourceRoot, function( hitElement ) 
local randomfunctionfromtable = functionstable[math.random(1,#functionstable)] 
   if ( getElementType( hitElement ) == 'vehicle' )  then 
 setTimer (_G[randomfunctionfromtable], 50, 1) 
 sx,sy,sz = getElementPosition(source) 
destroyElement(source) 
  end 
end 
) 

/debugscript 3

tell me the error if there is ,

Well, i learned something new today. Never test scripts via the map editor. On a server it works perfectly fine.

Anyway, thanks for your help!

Link to comment

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.
×
×
  • Create New...