Jump to content

Locking cars to Classes


joriss11

Recommended Posts

Hey Guys,

im not an scripter and i wanted to know if you guys maybe could explain/give me a good script to lock cars to classes.

because me and a Friend making an Server and it would be nice to have some cars that we can lock to gangs. ( no teams )

I hope you guys could help me.

PS. it would be nice if we could add the vehicles simply with a command or GUI but we don't mind to use the Ingame editor to place some.

Thx, anyways guys

Link to comment

You need:

onClientVehicleEnter to detect when someone enters the car, you then want to cancelEvent to prevent them from entering. Since car locks are synced (iirc) you can't use locking mechanisms to prevent certain players from entering. You might want to use outputChatBox to say why they can't enter.

Create a function that detects if a player is in a gang. Something like "getPlayerGang". When creating vehicles set an element data saying which gang owns it. Then when entering compare getPlayerGang with that element data and only cancel if they aren't the same.

Good luck.

This isn't a request forum, if you want this, you must either learn to make it by yourself, or pay someone to do it for you.

Well then you could've just explained it to him, like he said. He asked for "someone to explain how to create it for him, but to give him the script if possible".

Link to comment

Okay. So first I'll explain the event.

  
function limitVehiclesToGang(player) 
    local userGang = getElementData(player, "gang") -- This needs to be set 
    local vehicleOwner = getElementData(source, "gang") -- This needs to be set 
    if vehicleOwner and (userGang ~= vehicleOwner) then 
        outputChatBox("You don't own this vehicle.", player) 
        cancelEvent() 
    end 
end 
addEventHandler("onClientVehicleEnter", root, limitVehiclesToGang) 
  

You want to modify the message, and make sure that the element data is set.

The GUI seems really simple.

Link to comment

something like this? :

Vehicle = (506, 2100.2697, 1397.4947, 10.3, 0, 0, 0) 
  
addEventHandler("onVehicleStartEnter", root, 
 function ( thePlayer ) 
  local vehicleOwner = getElementData ( class, "Chief" ); 
  local gangGroup = getElementData ( thePlayer, "Chief" ); 
  if not ( vehicleOwner == gangGroup ) then 
   outputChatBox ("This car is Locked and belongs to the Following Class : Chief", thePlayer); 
   cancelEvent(); 
  end 
 end 

Link to comment

if i try this :

createVehicle = (506, 2100.2697, 1397.4947, 10.3, 0, 0, 0) 
  
addEventHandler("onVehicleStartEnter", root) 
 function ( thePlayer ) 
  local vehicleOwner = getElementData ( class, "Chief" ) 
  local gangGroup = getElementData ( thePlayer, "Chief" ) 
  if not ( vehicleOwner == gangGroup ) then 
   outputChatBox ("This car is Locked and belongs to the Following Class : Chief", thePlayer) 
   cancelEvent() 
  end 
 end 

then it won't work.

Link to comment
if i try this :
createVehicle = (506, 2100.2697, 1397.4947, 10.3, 0, 0, 0) 
  
addEventHandler("onVehicleStartEnter", root) 
 function ( thePlayer ) 
  local vehicleOwner = getElementData ( class, "Chief" ) 
  local gangGroup = getElementData ( thePlayer, "Chief" ) 
  if not ( vehicleOwner == gangGroup ) then 
   outputChatBox ("This car is Locked and belongs to the Following Class : Chief", thePlayer) 
   cancelEvent() 
  end 
 end 

then it won't work.

You made createVehicle a variable index. You're supposed to make variable for the createVehicle function instead. You also never defined the function within the event handler. Also, there is no "class" vehicle defined in the function. The vehicle is defined "source" by the Wiki.

--setElementData(getPlayerFromName("yournickhere"), "Chief", true, false) -- Uncomment this in order to make yourself a chief 
  
local vehicle = createVehicle(506, 2100.2697, 1397.4947, 10.3, 0, 0, 0) -- Create the vehicle 
setElementData(vehicle, "Chief", true, false) -- Give it element data in 'Chief' and make its value a boolean 'true' 
  
addEventHandler("onVehicleStartEnter", root, 
    function(thePlayer, seat, jacked, door) 
        local vehicleOwner = getElementData(source, "Chief") -- Get the vehicle's element data on 'Chief' 
        local gangGroup = getElementData(thePlayer, "Chief") -- Get the entering player's element data on 'Chief' 
        if (not vehicleOwner) then return end -- If the vehicle doesn't have such data, then cancel 
        if (gangGroup ~= vehicleOwner) then -- If the entering player's data doesn't match the vehicle data, continue 
            outputChatBox("This car is locked and belongs to the following class: Chief", thePlayer, 245, 20, 20, false) -- Display a warning 
            cancelEvent() -- Prevent the player from entering the vehicle 
        end 
    end 
) 

Link to comment

I actually used this code as server.lua . I changed my Coördinates to the location i anted, and the Vehicle is placed there now.

I didn't change anything except the Coördinates like i said and it almost works. its lcoked to Chief but when i spawn as the Class : Chief then it is still locked to Chief and i can't enter

Link to comment

Like this then?

server.lua :

setElementData(getPlayerFromName("Joris" or "joriss11"), "Chief", true, false) -- Uncomment this in order to make yourself a chief 
  
local vehicle = createVehicle(506, 2100.2697, 1397.4947, 10.3, 0, 0, 0) -- Create the vehicle 
setElementData(vehicle, "Chief", true, false) -- Give it element data in 'Chief' and make its value a boolean 'true' 
  
addEventHandler("onVehicleStartEnter", root, 
    function(thePlayer, seat, jacked, door) 
        local vehicleOwner = getElementData(source, "Chief") -- Get the vehicle's element data on 'Chief' 
        local gangGroup = getElementData(thePlayer, "Chief") -- Get the entering player's element data on 'Chief' 
        if (not vehicleOwner) then return end -- If the vehicle doesn't have such data, then cancel 
        if (gangGroup ~= vehicleOwner) then -- If the entering player's data doesn't match the vehicle data, continue 
            outputChatBox("This car is locked and belongs to the following Class: Chief", thePlayer, 245, 20, 20, false) -- Display a warning 
            cancelEvent() -- Prevent the player from entering the vehicle 
        end 
    end 
) 

its still locked and its still not working when im spawned as Chief

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