Jump to content

VEHICLE PANNEL SITUATION


lockdw

Recommended Posts

Hello there! Guys, i have a vehicle pannel where you can lock any car. But some players are locking job cars, how can i solve it? Maybe with a function that prevents the lock function by the vehicle ID. Could you guys help me? Thx.

function lockvehicle(source)
local Vehicle= getPedOccupiedVehicle(source)
if not Vehicle then outputChatBox("#0040ff✘#ffffffBVM#0040ff✘➺ #ffffffVocê não está em nenhum vehicle!",source,255,0,0,true) return end
local Seat = getPedOccupiedVehicleSeat(source)
local PlayerID = getElementData(source, "ID") 
if not PlayerID then return end
if Seat  == 0 then
if not getElementData(Vehicle, "TS:Locked") == true then 
setElementData(Vehicle, "TS:Locked", true)
setElementData(Vehicle, "TS:VehicleOwner", PlayerID)
outputChatBox("#0040ff✘#ffffffBVM#0040ff✘➺ #ffffffVehicle Locked!",source,255,0,0,true)
else
setElementData(Vehicle, "TS:Locked", false)
setElementData(Vehicle, "TS:VehicleOwner", false)
outputChatBox("#0040ff✘#ffffffBVM#0040ff✘➺ #ffffffVehicle Unlocked!",source,255,0,0,true)
end
else
outputChatBox("#0040ff✘#ffffffBVM#0040ff✘➺ #ffffffYou are not the vehicle driver!",source,255,0,0,true)
end
end
addEvent("TS:LockVehicle", true)
addEventHandler("TS:LockVehicle", root, LockVehicle)

function VehicleLocked(player)
local PlayerID = getElementData(player, "ID") 
if getElementData(source, "TS:Locked") == true then 
if getElementData(source, "TS:VehicleOwner") == PlayerID then return end
outputChatBox("#0040ff✘#ffffffBVM#0040ff✘➺ #ffffffThe Vehicle is Locked!",player,255,0,0,true) 
cancelEvent()
end
end
addEventHandler("onVehicleStartEnter", root, VehicleLocked)

 

 

Link to comment
10 hours ago, lockdw said:

Hello there! Guys, i have a vehicle pannel where you can lock any car. But some players are locking job cars, how can i solve it? Maybe with a function that prevents the lock function by the vehicle ID. Could you guys help me? Thx.


function lockvehicle(source)
local Vehicle= getPedOccupiedVehicle(source)
if not Vehicle then outputChatBox("#0040ff✘#ffffffBVM#0040ff✘➺ #ffffffVocê não está em nenhum vehicle!",source,255,0,0,true) return end
local Seat = getPedOccupiedVehicleSeat(source)
local PlayerID = getElementData(source, "ID") 
if not PlayerID then return end
if Seat  == 0 then
if not getElementData(Vehicle, "TS:Locked") == true then 
setElementData(Vehicle, "TS:Locked", true)
setElementData(Vehicle, "TS:VehicleOwner", PlayerID)
outputChatBox("#0040ff✘#ffffffBVM#0040ff✘➺ #ffffffVehicle Locked!",source,255,0,0,true)
else
setElementData(Vehicle, "TS:Locked", false)
setElementData(Vehicle, "TS:VehicleOwner", false)
outputChatBox("#0040ff✘#ffffffBVM#0040ff✘➺ #ffffffVehicle Unlocked!",source,255,0,0,true)
end
else
outputChatBox("#0040ff✘#ffffffBVM#0040ff✘➺ #ffffffYou are not the vehicle driver!",source,255,0,0,true)
end
end
addEvent("TS:LockVehicle", true)
addEventHandler("TS:LockVehicle", root, LockVehicle)

function VehicleLocked(player)
local PlayerID = getElementData(player, "ID") 
if getElementData(source, "TS:Locked") == true then 
if getElementData(source, "TS:VehicleOwner") == PlayerID then return end
outputChatBox("#0040ff✘#ffffffBVM#0040ff✘➺ #ffffffThe Vehicle is Locked!",player,255,0,0,true) 
cancelEvent()
end
end
addEventHandler("onVehicleStartEnter", root, VehicleLocked)

 

 

 

6 hours ago, The_GTA said:

Hello lockdw,

could you please use the "<>" button to format your Lua code properly? Otherwise you make it hard for people to read your code.

- Martin

My bad.

Link to comment
2 hours ago, lockdw said:

My bad.

Please just remember for the next time, then it's no problem :)

I tried out your script and I have the following question:
1) if a player locks a car and leaves it, how does he unlock the car? if I test the script then the player cannot re-enter the car.

So I implemented a job car system into your script. You can register vehicle model IDs as fixed job cars and spawn individual jobcars using the /sjc command.

client.Lua

addCommandHandler("lock",
    function()
        triggerServerEvent("TS:LockVehicle", root);
    end
);

addCommandHandler("sjc",
    function(cmdname, id)
        if not (id) then return; end;
        
        outputChatBox("spawning: " .. id);
    
        triggerServerEvent("TS:SpawnJobCar", root, tonumber(id));
    end
);

server.Lua

local always_job_car_ids = {
    [420] = true
};

local spawned_job_cars = {};

local function isjobcar(vehicle)
    local veh_id = getElementModel(vehicle);
    
    if (always_job_car_ids[veh_id]) then
        return true;
    end
    
    if (spawned_job_cars[vehicle]) then
        return true;
    end
    
    return false;
end

local function spawn_job_car(model, x, y, z)
    local veh = createVehicle(model, x, y, z);
    
    spawned_job_cars[veh] = true;
    
    addEventHandler("onClientElementDestroy", veh,
        function()
            spawned_job_vars[veh] = nil;
        end
    );
    
    return veh;
end

addEvent("TS:SpawnJobCar", true);
addEventHandler("TS:SpawnJobCar", root, function(id)
        if not (id) then return; end;

        local px, py, pz = getElementPosition(client);
        
        spawn_job_car(id, px, py + 5, pz + 2);
    end
);

function lockvehicle()
    local Vehicle= getPedOccupiedVehicle(client)
    if not Vehicle then
        outputChatBox("#0040ff?#ffffffBVM#0040ff?? #ffffffVocê não está em nenhum vehicle!",client,255,0,0,true)
        return
    end
    if (isjobcar(Vehicle)) then
        outputChatBox("#0040ff?#ffffffBVM#0040ff?? #ffffffCannot lock job car.", client, 255, 0, 0, true);
        return;
    end
    local Seat = getPedOccupiedVehicleSeat(client)
    --local PlayerID = getElementData(client, "ID") 
    --if not PlayerID then return end
    local PlayerID = getPlayerName(client);
    if Seat  == 0 then
        if not getElementData(Vehicle, "TS:Locked") == true then 
            setElementData(Vehicle, "TS:Locked", true)
            setElementData(Vehicle, "TS:VehicleOwner", PlayerID)
            outputChatBox("#0040ff?#ffffffBVM#0040ff?? #ffffffVehicle Locked!",client,255,0,0,true)
        else
            setElementData(Vehicle, "TS:Locked", false)
            setElementData(Vehicle, "TS:VehicleOwner", false)
            outputChatBox("#0040ff?#ffffffBVM#0040ff?? #ffffffVehicle Unlocked!",client,255,0,0,true)
        end
    else
        outputChatBox("#0040ff?#ffffffBVM#0040ff?? #ffffffYou are not the vehicle driver!",client,255,0,0,true)
    end
end
addEvent("TS:LockVehicle", true)
addEventHandler("TS:LockVehicle", root, lockvehicle)

function VehicleLocked(player)
local PlayerID = getElementData(player, "ID") 
    if getElementData(source, "TS:Locked") == true then 
        if getElementData(source, "TS:VehicleOwner") == PlayerID then return end
        outputChatBox("#0040ff?#ffffffBVM#0040ff?? #ffffffThe Vehicle is Locked!",player,255,0,0,true) 
        cancelEvent()
    end
end
addEventHandler("onVehicleStartEnter", root, VehicleLocked)

The always_job_car_ids table holds model numbers that will always be job cars. An example is the taxi car which has been added to the list by me. You can dynamically or remove a model number using:

-- Add the Landstalker.
always_job_car_ids[400] = true;

-- Remove the Landstalker.
always_job_car_ids[400] = nil;

If you call the spawn_job_car function then you can create a single unique job car (for example cars used during a mission/raid) that cannot be locked. Feel free to use this concept if you like it ;)

I have to mention a very important change to your script about your usage of source in the "TS:LockVehicle" event. Did you know that hackers can put the source element at their own wish, possibly locking the car of an admin player? Please use the global variable "client" to fix this problem. This way the player can only lock or unlock his own car.

- Martin

Edited by The_GTA
Link to comment
2 minutes ago, The_GTA said:

Please just remember for the next time, then it's no problem :)

I tried out your script and I have the following question:
1) if a player locks a car and leaves it, how does he unlock the car? if I test the script then the player cannot re-enter the car.

So I implemented a job car system into your script. You can register vehicle model IDs as fixed job cars and spawn individual jobcars using the /sjc command.

client.Lua


addCommandHandler("lock",
    function()
        triggerServerEvent("TS:LockVehicle", root);
    end
);

addCommandHandler("sjc",
    function(cmdname, id)
        if not (id) then return; end;
        
        outputChatBox("spawning: " .. id);
    
        triggerServerEvent("TS:SpawnJobCar", root, tonumber(id));
    end
);

server.Lua


local always_job_car_ids = {
    [420] = true
};

local spawned_job_cars = {};

local function isjobcar(vehicle)
    local veh_id = getElementModel(vehicle);
    
    if (always_job_car_ids[veh_id]) then
        return true;
    end
    
    if (spawned_job_cars[vehicle]) then
        return true;
    end
    
    return false;
end

local function spawn_job_car(model, x, y, z)
    local veh = createVehicle(model, x, y, z);
    
    spawned_job_cars[veh] = true;
    
    addEventHandler("onClientElementDestroy", veh,
        function()
            spawned_job_vars[veh] = nil;
        end
    );
    
    return veh;
end

addEvent("TS:SpawnJobCar", true);
addEventHandler("TS:SpawnJobCar", root, function(id)
        if not (id) then return; end;

        local px, py, pz = getElementPosition(client);
        
        spawn_job_car(id, px, py + 5, pz + 2);
    end
);

function lockvehicle()
    local Vehicle= getPedOccupiedVehicle(client)
    if not Vehicle then
        outputChatBox("#0040ff?#ffffffBVM#0040ff?? #ffffffVocê não está em nenhum vehicle!",client,255,0,0,true)
        return
    end
    if (isjobcar(Vehicle)) then
        outputChatBox("#0040ff?#ffffffBVM#0040ff?? #ffffffCannot lock job car.", client, 255, 0, 0, true);
        return;
    end
    local Seat = getPedOccupiedVehicleSeat(client)
    --local PlayerID = getElementData(client, "ID") 
    --if not PlayerID then return end
    local PlayerID = getPlayerName(client);
    if Seat  == 0 then
        if not getElementData(Vehicle, "TS:Locked") == true then 
            setElementData(Vehicle, "TS:Locked", true)
            setElementData(Vehicle, "TS:VehicleOwner", PlayerID)
            outputChatBox("#0040ff?#ffffffBVM#0040ff?? #ffffffVehicle Locked!",client,255,0,0,true)
        else
            setElementData(Vehicle, "TS:Locked", false)
            setElementData(Vehicle, "TS:VehicleOwner", false)
            outputChatBox("#0040ff?#ffffffBVM#0040ff?? #ffffffVehicle Unlocked!",client,255,0,0,true)
        end
    else
        outputChatBox("#0040ff?#ffffffBVM#0040ff?? #ffffffYou are not the vehicle driver!",client,255,0,0,true)
    end
end
addEvent("TS:LockVehicle", true)
addEventHandler("TS:LockVehicle", root, lockvehicle)

function VehicleLocked(player)
local PlayerID = getElementData(player, "ID") 
    if getElementData(source, "TS:Locked") == true then 
        if getElementData(source, "TS:VehicleOwner") == PlayerID then return end
        outputChatBox("#0040ff?#ffffffBVM#0040ff?? #ffffffThe Vehicle is Locked!",player,255,0,0,true) 
        cancelEvent()
    end
end
addEventHandler("onVehicleStartEnter", root, VehicleLocked)

The always_job_car_ids table holds model numbers that will always be job cars. An example is the taxi car which has been added to the list by me. You can dynamically or remove a model number using:


-- Add the Landstalker.
always_job_car_ids[400] = true;

-- Remove the Landstalker.
always_job_car_ids[400] = nil;

If you call the spawn_job_car function then you can create a single unique job car (for example cars used during a mission/raid) that cannot be locked. Feel free to use this concept if you like it ;)

- Martin

Answering you question: Actually he don't unlock the car, but the player who locks it can enter normally.

And thank you so much Martin. You're the guy!

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