Jump to content

Marker on spawned car


Recommended Posts

Hello ,

I tried to make a marker on my spawned car but i cant seem to get it working

I dont know if it will work since i couldnt get the script running so here it is :

  
local markername = { 
    [1] = { marker1 }, 
    [2] = { marker2 }, 
    [3] = { marker3 }, 
    [4] = { marker4 }, 
    [5] = { marker5}, 
    [6] = { marker6 }, 
    [7] = { marker7}, 
    [8] = { marker8 }, 
    [9] = { marker9}, 
    [10] = { marker10}, 
    [11] = { marker11} 
    } 
     
function spawncar() 
spawnedcar = createVehicle ( 432, 1746.59534,2504.34888,7.60135 ) 
setElementData(spawnedcar,"spawnedcar") 
local vehicle = getElementData(vehicle,"spawnedcar") 
local markername = markername[ math.random( #markername )  
markername = createMarker ( 0, 0, 0, "cylinder", 1.5, 255, 255, 0, 170 ) 
attachElements ( markername, vehicle, 0, 0, 5 ) 
end 
addCommandHandler("spawncar",spawncar) 
     
     
  
  
  

loading script failed "]" exepted near markername is the error i get

Edited by Guest
Link to comment

Thanks didnt see i missed a ]

i got other problem for setelementdata and more

for setelementdata :

exepted argument at argument 3 , got none ,

getelementdata:

exepted element at argument 1

attachelelements :

exepted element at argument 2 got boolean

Greetings

Link to comment

Im trying to make a script that adds a marker to the car wich is removeable and addable each time i use the script

for example if i spawn a car it gets an "name" and an marker added on the carif the car gets destroyed the marker should be gone also but should be useable to other cars

if its easier something like al cars exept some emergency cars should have a marker

i just did an example now with spawning a rhino but i want it all cars exept emergency cars

I hope u understund it ,

Thank you

Link to comment

Thank you so basicly it gonna look something like this :

  
notAllowedVehicle = { 
    [416] = true -- Ambulance 
} 
  
  
function test () 
local theVehicle = getPedOccupiedVehicle ( source )  
-- you must check the car model 
local model = getElemetModel(theVehicle) 
if (not allowedVehicle[model]) then 
-- nothing has to be done for not allowed vehicles 
else 
markers = createMarker( 0,0,0,"cylinder",1.5,255,255,0,170) 
attachElements ( markers, model, 0, 0, 5 ) 
end 
  
end 
 addEventHandler("onResourceStart",getRootElement(),test) 
  
  
  

this code didnt work out ^

if i do so i get

getpedoccupiedvehicle exepted ped at argument 1 got resource

Sorry for this but im not so good in this one

Greetings ,

Jonas

Link to comment

You code is wrong try this

notAllowedVehicle = { 
    [416] = true -- Ambulance 
} 
  
function attachMarker(player) 
    if not isElement(player) then return end  
        local theVehicle = getPedOccupiedVehicle ( player ) 
        if theVehicle then  
        local model = getElemetModel(theVehicle) 
        if (notAllowedVehicle[model]) then  
            outputChatBox("you can't attach a marker to this type of vehicles",player,255,0,0) 
        return  
        end  
        markers = createMarker( 0,0,0,"cylinder",1.5,255,255,0,170) 
        attachElements ( markers, theVehicle, 0, 0, 5 ) 
    end 
end 
addCommandHandler("attach",attachMarker) 

Type /attach in chatbox then press enter.

Link to comment

Hi thank you to help me

I got an error after trying ur script

attempt to call global 'getElementModel' a nil valeu

it works after removing the getelementmodel things (not a big problem if it goes to emergency cars then)

So now my estion is should i set a timer on the function so it checks every amount of minutes if there are cars and if so it adds a marker?

things i tried ;

  
  
  
function attachMarker(player) 
    if not isElement(player) then return end 
        local theVehicle = getElementsByType("vehicle") 
        if theVehicle then 
        --        local model = getElemetModel(theVehicle) 
        --     if (notAllowedVehicle[model]) then 
        --     outputChatBox("you can't attach a marker to this type of vehicles",player,255,0,0) 
        -- return 
        -- end 
        markers = createMarker( 0,0,0,"cylinder",1.5,255,255,0,170) 
        attachElements ( markers, theVehicle, 0, 0, 0 ) 
    end 
end 
addCommandHandler("attach",attachMarker) 
  
  

errors :

exepted element at argument 2 got table

Greetings

Link to comment

Hello ,

That worked thanks but now i still have the problem with adding a marker to ALL cars in once (may need a timer every 15 mins or so to check for cars nd add marker if needed) I tought using getElementByType("vehicle") but then it gives an error on attachelement that argument 2 got a table

thanks to help!

Greetings ,

Jonas

Link to comment
Hello ,

That worked thanks but now i still have the problem with adding a marker to ALL cars in once (may need a timer every 15 mins or so to check for cars nd add marker if needed) I tought using getElementByType("vehicle") but then it gives an error on attachelement that argument 2 got a table

thanks to help!

Greetings ,

Jonas

try this

local markers = {} 
local marker = createMarker( 0,0,0,"cylinder",1.5,255,255,0,170) 
  
function attchMarkerToAllVehicle () 
    for i, vehicle in pairs(getElementsByType("vehicle")) do 
        if not markers[vehicle] then  
            attachElements ( marker, vehicle, 0, 0, 0 ) 
            markers[vehicle] = true 
        end  
    end  
end 
setTimer(attchMarkerToAllVehicle,1000,0)  

Link to comment

Hm i get an error ;

attempt to index global 'markers' a nil value

might be due local marker = {} ?

  
local markers = {} 
-- local marker = createMarker( 0,0,0,"cylinder",1.5,255,255,0,170) 
  
function attchMarkerToAllVehicle () 
    for i, vehicle in pairs(getElementsByType("vehicle")) do 
    if not markers[vehicle] then 
        local marker = createMarker( 0,0,0,"cylinder",1.5,255,255,0,170) 
            attachElements ( marker, vehicle, 0, 0, 0 ) 
            markers[vehicle] = true 
        end 
    end 
end 
setTimer(attchMarkerToAllVehicle,1000,0) 
  

got it working thanks! but ambulances are also getting an marker but thats okay i will find it out with the getElementModel

Thank you very much!

greetings

Link to comment

Hello ,

Sorry i got a little question if i want to make someting like destroy all markers if a car gets deleted how will it be?

I tought something like ;

  
   function removemarker() 
    destroyElement(marker) 
    end 
     
    addEventHandler("onElementDestroy",getRootElement(),removemarker) 
  

Link to comment

Try this :

local markers = {} 
  
function attchMarkerToAllVehicle () 
    for i, vehicle in pairs(getElementsByType("vehicle")) do 
        if not markers[vehicle] then 
            local marker = createMarker( 0,0,0,"cylinder",1.5,255,255,0,170) 
            attachElements ( marker, vehicle, 0, 0, 0 ) 
            markers[vehicle] = marker 
        end 
    end 
end 
setTimer(attchMarkerToAllVehicle,1000,0) 
  
function destroyMarker() 
    if getElementType(source) == "vehicle" then 
        if markers[source] then  
            destroyElement (markers[source]) 
        end  
    end  
end  
addEventHandler("onElementDestroy", getRootElement(),destroyMarker) 

Link to comment
local markers = {} 
  
function attchMarkerToAllVehicle () 
    for i, vehicle in pairs(getElementsByType("vehicle")) do 
        if not markers[vehicle] then 
            local marker = createMarker( 0,0,0,"cylinder",1.5,255,255,0,170) 
            attachElements ( marker, vehicle, 0, 0, 0 ) 
            markers[vehicle] = marker 
        end 
    end 
end 
setTimer(attchMarkerToAllVehicle,1000,0) 
  
function destroyMarker() 
    if getElementType(source) == "vehicle" then 
        if markers[source] and isElement(markers[source]) then 
            destroyElement (markers[source]) 
        end 
    end 
end 
addEventHandler("onElementDestroy", getRootElement(),destroyMarker) 

Link to comment
Ah i see it didnt work with the model check but worked without , thank you

Simply with models it can be like this:

local notAllowedVehicle = {[416] = true} -- Ambulance 
local markers = {} 
  
function attchMarkerToAllVehicle () 
    for i, vehicle in pairs(getElementsByType("vehicle")) do 
     local model = getElementModel(vehicle) 
     if notAllowedVehicle[model] then return end  
        if not markers[vehicle] then 
            local marker = createMarker( 0,0,0,"cylinder",1.5,255,255,0,170) 
            attachElements ( marker, vehicle, 0, 0, 0 ) 
            markers[vehicle] = marker 
        end 
    end 
end 
setTimer(attchMarkerToAllVehicle,1000,0) 

Link to comment

Ok so i added a thing so if the car explodes it removes the mraker also ,i s htere a way to add again a marker if the car gets respawned?

i tried

addEventHandler ( "onVehicleRespawn", getRootElement(),attchMarkerToAllVehicle) on the main function of markers but didnt work out

Link to comment
local markers = {} 
  
function attchMarkerToAllVehicle () 
    for i, vehicle in pairs(getElementsByType("vehicle")) do 
        if not markers[vehicle] then 
            local marker = createMarker( 0,0,0,"cylinder",1.5,255,255,0,170) 
            attachElements ( marker, vehicle, 0, 0, 0 ) 
            markers[vehicle] = marker 
        end 
    end 
end 
setTimer(attchMarkerToAllVehicle,1000,0) 
  
addEventHandler("onVehicleRespawn",root, 
function() 
    if not markers[source] then 
        local marker = createMarker( 0,0,0,"cylinder",1.5,255,255,0,170) 
        attachElements ( marker, source, 0, 0, 0 ) 
        markers[source] = marker 
    end 
end 
) 
  
function destroyMarker() 
    if getElementType(source) == "vehicle" then 
        if markers[source] and isElement(markers[source]) then 
            destroyElement (markers[source]) 
            markers[source] = nil 
        end 
    end 
end 
addEventHandler("onElementDestroy", getRootElement(),destroyMarker) 

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...