Jump to content

[HELP] Faction car system


opnaiC

Recommended Posts

Hello guys,

in the script below I made a table and a function that will create cars from the table information. (and this function is working)

Now I want to add a /lock and /engine system only for this cars. I made 2 functions below but they wont work because they are wrong. This cars are made for the LSPD faction and player should only lock or start the engine if he is in the faction. So I am asking for help here.

local pVeh = { 
    {model = 596, x = 1570.2998046875, y = -1710.099609375, z = 5.6999998092651, rx = 0, ry = 0, rz = 0, r = 65, g = 105, b = 255, lp = "SA-AA-01", r2 = 255, g2 = 255, b2 = 255}, 
    {model = 596, x = 1574.5, y = -1710.099609375, z =  5.6999998092651, rx = 0, ry = 0, rz = 0, r = 65, g = 105, b = 255, lp = "SA-AB-76", r2 = 255, g2 = 255, b2 = 255}, 
    {model = 426, x = 1578.5, y = -1710, z =  5.6999998092651, rx = 0, ry = 0, rz = 0, r = 192, g = 192, b = 192, lp = "SA-TC-31"},  -- 
    {model = 560, x = 1583.599609375, y = -1710.099609375, z =  5.6999998092651, rx = 0, ry = 0, rz = 0, r = 0, g = 0, b = 128, lp = "SA-KJ-81"},  -- 
    {model = 579, x = 1587.7001953125, y = -1710.2001953125, z =  5.6999998092651, rx = 0, ry = 0, rz = 0, r = 70, g = 0, b = 0, lp = "SA-CZ-48"}, -- 
    {model = 427, x = 1529.7998046875, y = -1683.7001953125, z =  6.0999999046326, rx = 0, ry = 0, rz = 270, r = 65, g = 105, b = 255, lp = "SA-UJ-40", r2 = 255, g2 = 255, b2 = 255}, 
    {model = 427, x = 1529.7001953125, y = -1687.7998046875, z = 6.0999999046326, rx = 0, ry = 0, rz = 270, r = 65, g = 105, b = 255, lp = "SA-KS-00", r2 = 255, g2 = 255, b2 = 255}, 
} 
  
function buildPVehicle () 
 for i,v in pairs (pVeh) do       
  pVehicle = createVehicle (v.model, v.x, v.y, v.z, v.rx, v.ry, v.rz, v.lp) 
  setVehicleLocked (pVehicle, false) 
  setVehicleColor (pVehicle, v.r, v.g, v.b, v.r2, v.g2, v.b2) 
 end 
end 
addEventHandler ("onResourceStart", root, buildPVehicle) 
  
function playerCarLock ( source, cmd) -- this function is wrong 
    for i,v in pairs (pVehicle) do 
      local playeraccount = getPlayerAccount ( source ) 
      local fac = getAccountData(playeraccount,"Faction") or 0 
    if (tonumber(fac) == 2) then 
      if getDistanceBetweenPoints3D (getElementPosition(pVehicle), getElementPosition(source)) 
            if not isVehicleLocked (pVehicle) then       
            setVehicleLocked (pVehicle, true ) 
            else 
            setVehicleLocked ( pVehicle, false ) 
            end      
        end 
    end 
end 
addCommandHandler ("lock",playerCarLock) 
  
function engine ( source, cmd) -- this function is wrong 
for i,v in pairs (pVehicle) do 
if getPedOccupiedVehicle(source) == pVehicle then 
        if (tonumber(fac) == 2) then 
            if getVehicleEngineState(pVehicle) == false then 
            setVehicleEngineState ( pVehicle, true ) 
            else 
            setVehicleEngineState ( pVehicle, false ) 
            end 
        end 
end      
end      
end 
addCommandHandler ("engine",engine) 
  
  
  
      

Link to comment

you should replace the "Lock" function with this one:

function playerCarLock(source) 
    local playeraccount = getPlayerAccount(source) 
    local fac = getAccountData(playeraccount, "Faction") or 0 
    for _, veh in ipairs (getElementsByType("vehicle", resourceRoot)) do 
        if (tonumber(fac) == 2) then 
            if (getDistanceBetweenPoints3D(getElementPosition(veh), getElementPosition(source)) < 6 ) then 
                if not (isVehicleLocked(veh)) then      
                    setVehicleLocked(veh, true) 
                else 
                    setVehicleLocked(veh, false) 
                end     
            end 
        end 
    end 
end 
addCommandHandler("lock", playerCarLock) 

I'm pretty sure it will work, if it doesn't it would help if you put the ERROR that pops-up in the debug.

note about script:

You'll lock every vehicle created by the resource that you posted that is under 6 points of distance

function engine(source) 
    local playeraccount = getPlayerAccount(source) 
    local fac = getAccountData(playeraccount, "Faction") or 0 
    for _, veh in ipairs (getElementsByType("vehicle", resourceRoot)) do 
        if (getPedOccupiedVehicle(source) == veh) and (tonumber(fac) == 2) then 
            if (getVehicleEngineState(veh) == false) then 
                setVehicleEngineState(veh, true) 
            else 
                setVehicleEngineState(veh, false) 
            end 
        end     
    end     
end 
addCommandHandler("engine", engine) 

This is for the engine

Link to comment

Okay I fixed one line and its working now. But like I understand this system will work for all created cars in this resource, right ? But then I would have a problem because all my factions are in one resource and I want to add this system to else of them.

Link to comment
function playerCarLock(source) 
    local playeraccount = getPlayerAccount(source) 
    local fac = getAccountData(playeraccount, "Faction") or 0 
    for vehicles in pairs (pVehicle) do 
        if (tonumber(fac) == 2) then 
            if (getDistanceBetweenPoints3D(getElementPosition(vehicles), getElementPosition(source)) < 6 ) then 
                if not (isVehicleLocked(vehicles)) then      
                    setVehicleLocked(vehicles, true) 
                else 
                    setVehicleLocked(vehicles, false) 
                end     
            end 
        end 
    end 
end 
addCommandHandler("lock", playerCarLock) 
  
function engine(source) 
    local playeraccount = getPlayerAccount(source) 
    local fac = getAccountData(playeraccount, "Faction") or 0 
    for vehicles in pairs (pVehicle) do 
        if (getPedOccupiedVehicle(source) == vehicles) and (tonumber(fac) == 2) then 
            if (getVehicleEngineState(vehicles) == false) then 
                setVehicleEngineState(vehicles, true) 
            else 
                setVehicleEngineState(vehicles, false) 
            end 
        end     
    end     
end 
addCommandHandler("engine", engine) 

If it doesn't work try to change this line:

local pVehicle = {} 
  
function buildPVehicle() 
    for _, v in ipairs (pVeh) do       
        table.insert(pVehicle, createVehicle(v.model, v.x, v.y, v.z, v.rx, v.ry, v.rz, v.lp)) 
        setVehicleLocked(pVehicle, false) 
        setVehicleColor(pVehicle, v.r, v.g, v.b, v.r2, v.g2, v.b2) 
    end 
end 
addEventHandler("onResourceStart", root, buildPVehicle) 

And if afterall it doesn't work, you should wait for the answer of someone with more knowledge than me cause I'm not very experienced atall.

Link to comment
function playerCarLock(source) 
    local playeraccount = getPlayerAccount(source) 
    local fac = getAccountData(playeraccount, "Faction") or 0 
    for vehicles in pairs (pVehicle) do 
        if (tonumber(fac) == 2) then 
            if (getDistanceBetweenPoints3D(getElementPosition(vehicles), getElementPosition(source)) < 6 ) then 
                if not (isVehicleLocked(vehicles)) then      
                    setVehicleLocked(vehicles, true) 
                else 
                    setVehicleLocked(vehicles, false) 
                end     
            end 
        end 
    end 
end 
addCommandHandler("lock", playerCarLock) 
  
function engine(source) 
    local playeraccount = getPlayerAccount(source) 
    local fac = getAccountData(playeraccount, "Faction") or 0 
    for vehicles in pairs (pVehicle) do 
        if (getPedOccupiedVehicle(source) == vehicles) and (tonumber(fac) == 2) then 
            if (getVehicleEngineState(vehicles) == false) then 
                setVehicleEngineState(vehicles, true) 
            else 
                setVehicleEngineState(vehicles, false) 
            end 
        end     
    end     
end 
addCommandHandler("engine", engine) 

If it doesn't work try to change this line:

local pVehicle = {} 
  
function buildPVehicle() 
    for _, v in ipairs (pVeh) do       
        table.insert(pVehicle, createVehicle(v.model, v.x, v.y, v.z, v.rx, v.ry, v.rz, v.lp)) 
        setVehicleLocked(pVehicle, false) 
        setVehicleColor(pVehicle, v.r, v.g, v.b, v.r2, v.g2, v.b2) 
    end 
end 
addEventHandler("onResourceStart", root, buildPVehicle) 

And if afterall it doesn't work, you should wait for the answer of someone with more knowledge than me cause I'm not very experienced atall.

I'm sorry but this is not correct.

So, let's do something like this :

  
-- First : Make this function in your script, she's realy usefull 
  
function table.contains(table, element) 
  for _, value in pairs(table) do 
    if value == element then 
      return true 
    end 
  end 
  return false 
end 
  
-- Ok now we'll define all the LSPD vehicles :  
  
lspdv = {596, 598, 427, 601, 599, 528, 490} -- You can add or remove some value(s) by this table. 
  
function lock (source) 
if not isPedInVehicle(source) then 
local x,y,z = getElementPosition(source) 
local colshape = createColShape(x,y,z,2) -- Ok, here the colshape is not too big but it's for don't have any bug 
local elements = getElementsWithinColShape(colshape, "vehicle") 
destroyElement(colshape) 
if #elements == 1 then -- So here the # is to count the number of vehicle in the colshape 
for _,v in ipairs(elements) do 
local model = getElementModel(v) 
if table.contains (lspdv, model) then -- We check if the vehicle near the player is the lspdv table 
-- Ok, here make you faction detection with "if" and after don't forget to make an end 
local locked = isVehicleLocked ( vh  ) 
local locked = not locked 
setVehicleLocked(vh, locked) 
end 
end 
else 
outputChatBox("Sorry, too much vehicles / any vehicle near you.", source, 255,0,0) 
end 
else 
local vh = getPedOccupiedVehicle(source) 
local model = getElementModel(source) 
if table.contains(lspdv, model) then 
-- Ok, here make you faction detection with "if" and after don't forget to make an end 
local locked = isVehicleLocked ( vh  ) 
local locked = not locked 
setVehicleLocked(vh, locked) 
end 
end 
end 
addCommandHandler("lock",lock) 

Ok, so now, make that for the engine. We'll see if you have learn something.

And ... You can join me by private message, I could help you ...

Link to comment
function playerCarLock(source) 
    local playeraccount = getPlayerAccount(source) 
    local fac = getAccountData(playeraccount, "Faction") or 0 
    for vehicles in pairs (pVehicle) do 
        if (tonumber(fac) == 2) then 
            if (getDistanceBetweenPoints3D(getElementPosition(vehicles), getElementPosition(source)) < 6 ) then 
                if not (isVehicleLocked(vehicles)) then      
                    setVehicleLocked(vehicles, true) 
                else 
                    setVehicleLocked(vehicles, false) 
                end     
            end 
        end 
    end 
end 
addCommandHandler("lock", playerCarLock) 
  
function engine(source) 
    local playeraccount = getPlayerAccount(source) 
    local fac = getAccountData(playeraccount, "Faction") or 0 
    for vehicles in pairs (pVehicle) do 
        if (getPedOccupiedVehicle(source) == vehicles) and (tonumber(fac) == 2) then 
            if (getVehicleEngineState(vehicles) == false) then 
                setVehicleEngineState(vehicles, true) 
            else 
                setVehicleEngineState(vehicles, false) 
            end 
        end     
    end     
end 
addCommandHandler("engine", engine) 

If it doesn't work try to change this line:

local pVehicle = {} 
  
function buildPVehicle() 
    for _, v in ipairs (pVeh) do       
        table.insert(pVehicle, createVehicle(v.model, v.x, v.y, v.z, v.rx, v.ry, v.rz, v.lp)) 
        setVehicleLocked(pVehicle, false) 
        setVehicleColor(pVehicle, v.r, v.g, v.b, v.r2, v.g2, v.b2) 
    end 
end 
addEventHandler("onResourceStart", root, buildPVehicle) 

And if afterall it doesn't work, you should wait for the answer of someone with more knowledge than me cause I'm not very experienced atall.

I'm sorry but this is not correct.

So, let's do something like this :

  
-- First : Make this function in your script, she's realy usefull 
  
function table.contains(table, element) 
  for _, value in pairs(table) do 
    if value == element then 
      return true 
    end 
  end 
  return false 
end 
  
-- Ok now we'll define all the LSPD vehicles :  
  
lspdv = {596, 598, 427, 601, 599, 528, 490} -- You can add or remove some value(s) by this table. 
  
function lock (source) 
if not isPedInVehicle(source) then 
local x,y,z = getElementPosition(source) 
local colshape = createColShape(x,y,z,2) -- Ok, here the colshape is not too big but it's for don't have any bug 
local elements = getElementsWithinColShape(colshape, "vehicle") 
destroyElement(colshape) 
if #elements == 1 then -- So here the # is to count the number of vehicle in the colshape 
for _,v in ipairs(elements) do 
local model = getElementModel(v) 
if table.contains (lspdv, model) then -- We check if the vehicle near the player is the lspdv table 
-- Ok, here make you faction detection with "if" and after don't forget to make an end 
local locked = isVehicleLocked ( vh  ) 
local locked = not locked 
setVehicleLocked(vh, locked) 
end 
end 
else 
outputChatBox("Sorry, too much vehicles / any vehicle near you.", source, 255,0,0) 
end 
else 
local vh = getPedOccupiedVehicle(source) 
local model = getElementModel(source) 
if table.contains(lspdv, model) then 
-- Ok, here make you faction detection with "if" and after don't forget to make an end 
local locked = isVehicleLocked ( vh  ) 
local locked = not locked 
setVehicleLocked(vh, locked) 
end 
end 
end 
addCommandHandler("lock",lock) 

Ok, so now, make that for the engine. We'll see if you have learn something.

And ... You can join me by private message, I could help you ...

veh is not defined in the first lines so it returns nil. how should I define it ?

Also the script is not better then the previous cause with this the police can open all police cars. As example when I would add the Savanna model in grove street they could open all models.

Link to comment
  
-- First : Make this function in your script, she's realy usefull 
  
function table.contains(table, element) 
  for _, value in pairs(table) do 
    if value == element then 
      return true 
    end 
  end 
  return false 
end 
  
-- Ok now we'll define all the LSPD vehicles :  
  
lspdv = {596, 598, 427, 601, 599, 528, 490} -- You can add or remove some value(s) by this table. 
  
function lock (source) 
if not isPedInVehicle(source) then 
local x,y,z = getElementPosition(source) 
local colshape = createColShape(x,y,z,2) -- Ok, here the colshape is not too big but it's for don't have any bug 
local elements = getElementsWithinColShape(colshape, "vehicle") 
destroyElement(colshape) 
if #elements == 1 then -- So here the # is to count the number of vehicle in the colshape 
for _,v in ipairs(elements) do 
local model = getElementModel(v) 
if table.contains (lspdv, model) then -- We check if the vehicle near the player is the lspdv table 
-- Ok, here make you faction detection with "if" and after don't forget to make an end 
local locked = isVehicleLocked ( vh  ) 
local locked = not locked 
setVehicleLocked(vh, locked) 
end 
end 
else 
outputChatBox("Sorry, too much vehicles / any vehicle near you.", source, 255,0,0) 
end 
else 
local vh = getPedOccupiedVehicle(source) 
local model = getElementModel(source) 
if table.contains(lspdv, model) then 
-- Ok, here make you faction detection with "if" and after don't forget to make an end 
local locked = isVehicleLocked ( vh  ) 
local locked = not locked 
setVehicleLocked(vh, locked) 
end 
end 
end 
addCommandHandler("lock",lock) 

Ok, so now, make that for the engine. We'll see if you have learn something.

And ... You can join me by private message, I could help you ...

veh is not defined in the first lines so it returns nil. how should I define it ?

Also the script is not better then the previous cause with this the police can open all police cars. As example when I would add the Savanna model in grove street they could open all models.

Mhh ... You asked something I gave you a big part of you script.

We'll (by "we" I say the MTA's forum) not script your gamemode for you. You must be sure of that.

And for my little error, yes.

Replace the line 30 by this :

setVehicleLocked(v, locked) 

Link to comment
  
-- First : Make this function in your script, she's realy usefull 
  
function table.contains(table, element) 
  for _, value in pairs(table) do 
    if value == element then 
      return true 
    end 
  end 
  return false 
end 
  
-- Ok now we'll define all the LSPD vehicles :  
  
lspdv = {596, 598, 427, 601, 599, 528, 490} -- You can add or remove some value(s) by this table. 
  
function lock (source) 
if not isPedInVehicle(source) then 
local x,y,z = getElementPosition(source) 
local colshape = createColShape(x,y,z,2) -- Ok, here the colshape is not too big but it's for don't have any bug 
local elements = getElementsWithinColShape(colshape, "vehicle") 
destroyElement(colshape) 
if #elements == 1 then -- So here the # is to count the number of vehicle in the colshape 
for _,v in ipairs(elements) do 
local model = getElementModel(v) 
if table.contains (lspdv, model) then -- We check if the vehicle near the player is the lspdv table 
-- Ok, here make you faction detection with "if" and after don't forget to make an end 
local locked = isVehicleLocked ( vh  ) 
local locked = not locked 
setVehicleLocked(vh, locked) 
end 
end 
else 
outputChatBox("Sorry, too much vehicles / any vehicle near you.", source, 255,0,0) 
end 
else 
local vh = getPedOccupiedVehicle(source) 
local model = getElementModel(source) 
if table.contains(lspdv, model) then 
-- Ok, here make you faction detection with "if" and after don't forget to make an end 
local locked = isVehicleLocked ( vh  ) 
local locked = not locked 
setVehicleLocked(vh, locked) 
end 
end 
end 
addCommandHandler("lock",lock) 

Ok, so now, make that for the engine. We'll see if you have learn something.

And ... You can join me by private message, I could help you ...

veh is not defined in the first lines so it returns nil. how should I define it ?

Also the script is not better then the previous cause with this the police can open all police cars. As example when I would add the Savanna model in grove street they could open all models.

Mhh ... You asked something I gave you a big part of you script.

We'll (by "we" I say the MTA's forum) not script your gamemode for you. You must be sure of that.

And for my little error, yes.

Replace the line 30 by this :

setVehicleLocked(v, locked) 

I am not asked anybody to script my gamemode I just asked for some help. The script you gave me didnt helped me so I used the one before. But thanks by the way))

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