Jump to content

Vehicle Blips


mgdmgd

Recommended Posts

Hey guys, I want some help here

I want this code to be only for spefic model of vehicles which is the police ones not all streamed-in vehicles

 


local vehicleBlipRoot = createElement("vehicleBlipRoot", "vehicleBlipRoot")

--This function creates a blip for all currently streamed-in vehicles when the resource starts.
local function resourceStart()
	for _, vehicle in ipairs(getElementsByType("vehicle"), root, true) do
		if vehicle ~= getPedOccupiedVehicle(localPlayer) then
			local blip = createBlipAttachedTo(vehicle, 0, 1, 150, 150, 150, 255, -10, 300)
			setElementParent(blip, vehicleBlipRoot)
		end
	end
end
addEventHandler("onClientResourceStart", root, resourceStart)

--This function destroys a vehicle's blip when it streams out.
local function streamOut()
	for _, blip in ipairs(getElementChildren(vehicleBlipRoot)) do
		if getElementAttachedTo(blip) == source then
			destroyElement(blip)
		end
	end
	removeEventHandler("onClientElementStreamOut", source, streamOut)
	removeEventHandler("onClientElementDestroy", source, streamOut)
end

--This function creates a blip when a vehicle streams in.
local function streamIn()
	if getElementType(source) ~= "vehicle" then
		return
	end
	
	--Check if the vehicle already has a blip.
	for _, blip in ipairs(getElementChildren(vehicleBlipRoot)) do
		if getElementAttachedTo(blip) == source then
			return
		end
	end
	
	local blip = createBlipAttachedTo(source, 0, 1, 150, 150, 150, 255, -10, 300)
	setElementParent(blip, vehicleBlipRoot)
	addEventHandler("onClientElementStreamOut", source, streamOut)
	addEventHandler("onClientElementDestroy", source, streamOut)
end
addEventHandler("onClientElementStreamIn", root, streamIn)

 

Link to comment

Simply use getElementModel, for example:
 

--This function creates a blip for all currently streamed-in vehicles when the resource starts.
local function resourceStart()
	for _, vehicle in ipairs(getElementsByType("vehicle"), root, true) do
		if vehicle ~= getPedOccupiedVehicle(localPlayer) then
      		if getElementModel(vehicle) == 596 then
				local blip = createBlipAttachedTo(vehicle, 0, 1, 150, 150, 150, 255, -10, 300)
				setElementParent(blip, vehicleBlipRoot)
       		end
		end
	end
end
addEventHandler("onClientResourceStart", root, resourceStart)

 

Link to comment

Or loop an array of ids:

local vehicleIds = {596, 597, 598, 599}

function attachBlipToVehicle(vehicle)
	if getPedOccupiedVehicle(localPlayer) ~= vehicle then
		for i=1, #vehicleIds do
			if vehicleIds[i] == getElementModel(vehicle) then
				vehicleBlips[vehicle] = createBlipAttachedTo(vehicle, 0, 1, 150, 150, 150, 255, -10, 300)
				setBlipVisibleDistance(vehicleBlips[vehicle], 50)
			end
		end
	end
end

 

Edited by Tails
Link to comment

Alright but I got faction system and I used it to be like this but didnt work


local vehicleBlipRoot = createElement("vehicleBlipRoot", "vehicleBlipRoot")

local vehicleIds = {596, 597, 598, 599}
function attachBlipToVehicle(vehicle)
        local police = exports.factions:getTeamFromFactionID(1) -- Get the team name of that faction
        local policeman = getPlayersInTeam(police) -- Get the players in a team
	if vehicle ~= getPedOccupiedVehicle(localPlayer) then
		for i=1, #vehicleIds do
			if getElementModel(vehicle) == vehicleIds[i] then
				vehicleBlips[vehicle] = createBlipAttachedTo(vehicle, 0, 1, 150, 150, 150, 255, -10, 300)
				setBlipVisibleDistance(vehicleBlips[vehicle], 50)
			end
		end
	end
end
--This function creates a blip for all currently streamed-in vehicles when the resource starts.
local function resourceStart()
        local police = exports.factions:getTeamFromFactionID(1) -- Get the team name of that faction
        local policeman = getPlayersInTeam(police) -- Get the players in a team
	for _, vehicle in ipairs(getElementsByType("vehicle"), root, true) do
		if vehicle ~= getPedOccupiedVehicle(localPlayer) then
      		if getElementModel(vehicle) == 596 then
				local blip = createBlipAttachedTo(vehicle, 0, 1, 150, 150, 150, 255, -10, 300)
				setElementParent(blip, vehicleBlipRoot)
       		end
		end
	end
end
addEventHandler("onClientResourceStart", root, resourceStart)
--This function destroys a vehicle's blip when it streams out.

As I want the blips only be shown to faction with ID 1

Can somebody help please.

@Hale @Tails

Or only show it to people that driving police cars

Link to comment
56 minutes ago, mgdmgd said:

@Tails yours didn't work but I merged the both scripts together and I got it to work

I want it to be shown only to those people that using police skins only, is that possible?

Use the same idea Tails showed you. Put police skins in a table, loop through it and check if the localPlayer has any of the police skins with getElementModel.

Link to comment

@mgdmgd I went ahead and scripted the whole thing of what I think you're trying to achieve.

Try it out and let me know if it's what you wanted.

local vehicleBlips = {}
local vehicleIds = {596, 597, 598, 599}

function attachBlipToVehicle(vehicle)
	if not getVehicleController(vehicle) then
		for i=1, #vehicleIds do
			if vehicleIds[i] == getElementModel(vehicle) then
				vehicleBlips[vehicle] = createBlipAttachedTo(vehicle, 0, 1, 150, 150, 150, 255, -10, 300)
				setBlipVisibleDistance(vehicleBlips[vehicle], 150)
			end
		end
	end
end

addEventHandler("onClientResourceStart", root,
	function()
		for _, vehicle in ipairs(getElementsByType("vehicle", root, true)) do
			attachBlipToVehicle(vehicle)
		end
	end
)

addEventHandler("onClientElementStreamIn", root,
	function()
		if getElementType(source) == "vehicle" then
			setTimer(
				function(vehicle)
					attachBlipToVehicle(vehicle)
				end, 
			100, 1, source)
		end
	end
)

addEventHandler("onClientElementStreamOut", root,
	function()
		if vehicleBlips[source] then
			destroyElement(vehicleBlips[source])
			vehicleBlips[source] = nil
		end
	end
)

You also made a small mistake, you had

for _, vehicle in ipairs(getElementsByType("vehicle"), root, true) do
  

but should be

for _, vehicle in ipairs(getElementsByType("vehicle", root, true)) do

Not sure if it had to do with your problem but anyway.

Also notice the timer when the vehicles stream in, it's necessary because it doesn't always pick it up. Attaching a blip right away will give errors.

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