Jump to content

Lock System


shaio

Recommended Posts

I've been working on this for a while now, but I think I'm a script kiddie, this works like shit was just thrown together :L Idk how to go any further with it. Is there anyway to enhance this??

function lock(player,cmd) 
    if isPedInVehicle(player) then  
        if getPedOccupiedVehicleSeat(player) == 0 then 
            if (getElementData(getPedOccupiedVehicle(player),"locks") == true) then 
                setElementData(getPedOccupiedVehicle(player),"lock",false) 
                setElementData(getPedOccupiedVehicle(player),"locks",false) 
                setVehicleLocked(getPedOccupiedVehicle(player),false) 
                outputChatBox("Unlocked!",player,0,255,255) 
            else 
                setElementData(getPedOccupiedVehicle(player),"lock",getPlayerSerial(player)) 
                setElementData(getPedOccupiedVehicle(player),"locks",true) 
                outputChatBox("Locked!",player,0,255,255) 
            end 
        else 
            outputChatBox("You are not the driver!",player,0,255,255) 
        end 
    end 
end 
addCommandHandler("lock",lock) 
  
addEventHandler ("onPlayerJoin", getRootElement(), 
function() 
    bindKey (source, "k", "down", lock) 
end) 
  
addEventHandler ("onResourceStart", getResourceRootElement(getThisResource()), 
function() 
    local players = getElementsByType ("player") 
    for k, player in ipairs (players) do 
        bindKey (player, "k", "down", lock) 
    end 
end) 
  
function inveh(player,seat,jacked) 
local accountName = getAccountName(getPlayerAccount(player)) 
if isObjectInACLGroup("user."..accountName,aclGetGroup("Admin")) or isObjectInACLGroup("user."..accountName,aclGetGroup("SuperModerator")) or isObjectInACLGroup("user."..accountName,aclGetGroup("Moderator"))then 
    return 
else 
    if not (getElementData(source,"locked") == getPlayerSerial(player)) then 
        if (getElementData(source,"lockeds") == false) then 
            return 
        else 
            if (seat == 0) then 
                cancelEvent() 
                outputChatBox("This is not your vehicle!",player,0,255,255) 
            end 
        end 
    end 
end 
end 
addEventHandler("onVehicleStartEnter",getRootElement(),inveh) 
  
function share(player,cmd) 
    if (getVehicleOccupant(getPedOccupiedVehicle(player),0) == player) then 
        setElementData(getPedOccupiedVehicle(player),"lockeds",false) 
        setElementData(getPedOccupiedVehicle(player),"lock",false) 
        outputChatBox("Vehicle is now shared with everyone!",player,0,255,255) 
    else 
        outputChatBox("You need to be the driver!",player,0,255,255) 
    end 
end 
addCommandHandler("share",share) 
  
function ejectPlayer(player,cmd,eject) 
    if (getVehicleOccupant(getPedOccupiedVehicle(player),0) == player) then 
        removePlayerFromVehicle(getPlayerFromPartialName(eject)) 
        outputChatBox(getPlayerName(player).." has removed you from the vehicle!",getPlayerFromPartialName(eject),0,255,255) 
    else 
        outputChatBox("You need to be the driver!",player,0,255,255) 
    end 
end 
addCommandHandler("eject",ejectPlayer) 
         
         
function getPlayerFromPartialName(name) 
    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil 
    if name then 
        for _, player in ipairs(getElementsByType("player")) do 
            local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower() 
            if name_:find(name, 1, true) then 
                return player 
            end 
        end 
    end 
end 

Link to comment

If I got it, you just want suggestions to improve your resource, right?

I think you should save your lock status into a database, so it'll stay after server restart.

Also definitely add option to lock your vehicle from outside.

But I'm not sure if my suggestions are good for you, since you didn't write what gamemode is this for. RPG? Freeroam?

Link to comment

first question (yes)

second question (freeroam)

I run my own freeroam/drift/rp/zombies gamemode server, its kinda just a bunch of shit piled on xD, but it did turn out pretty decent. This lock script to me just seems like a piece of mumbo jumbo. How would i make /share so u could share with a specific person, enable and disable it for that person and what not.. I also dont like how everytime a person enters a car, the data changes to their serial..

Link to comment

Firstly, I think you should use tables and not elemnt datas since it'll mess things up (in my opinion at least) and tables are made for such things.

So you can do something like this:

  
local players = {shares = {}} 
  

now everytime you want someone to lock, you insert the player this way:

  
players [player] = {} 
  

and everytime you want to share the table with someone, you write:

  
for _,v in pairs (players) do 
    table.insert (v.shares, sharedPlayer) 
end 
  

now when a player wants to get in the car, you can check it this way (if he's allowed)

  
for _,v in pairs (players) do 
    for _,a in pairs (v.shares) do 
        if (a == specifiedPlayerWhoWantsToJoinTheCar) then 
            --allow him 
        end 
    end 
end 
  

and by the way, don't you need a function to remove the player from `car share` ?

Link to comment

I used (would personally use) player element instead of serial, but if that's how you really want it:

  
players = {}  
  
local currentSerial = getPlayerSerial (player) 
  
players [currentSerial] = {shares = {}, lockable = false / true and anything you want here} 
  

Link to comment

This is what I have..

local players = {shares = {}} 
  
addEventHandler("onPlayerJoin",getRootElement(),function() 
    bindKey(source,"k","down","lock") 
end) 
  
addEventHandler("onResourceStart",getRootElement(),function() 
    for _,plr in ipairs(getElementsByType("player")) do 
        bindKey(plr,"k","down","lock") 
    end 
end) 
  
function lockVehicle(player,cmd) 
    local vehicle = getPedOccupiedVehicle(player) 
    if isVehicleLocked(vehicle) then 
        setVehicleLocked(vehicle,false) 
    else 
        setVehicleLocked(vehicle,true) 
        players[player] = {} 
    end 
end 
addCommandHandler("lock",lockVehicle) 
  
function shareLock(player,cmd,other) 
    local serial = getPlayerSerial(getPlayerFromPartialName(other)) 
    for _,v in pairs(players) do 
        table.insert(v.shares,serial) 
    end 
end 
addCommandHandler("allow",shareLock) 
  
addEventHandler("onVehicleStartEnter",getRootElement(),function(player,seat,jacked) 
    for _,v in pairs(players) do 
        for _,a in pairs(v.shares) do 
            if (a == player) then 
                return 
            else 
                cancelEvent() 
            end 
        end 
    end 
end) 
  
function getPlayerFromPartialName(name) 
    local name = name and name:gsub("#%x%x%x%x%x%x",""):lower() or nil 
    if name then 
        for _,player in ipairs(getElementsByType("player")) do 
            local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x",""):lower() 
            if name_:find(name,1,true) then 
                return player 
            end 
        end 
    end 
end 

Link to comment
  
local players = {cars = {}, unlockedFor = {}} 
  
addCommandHandler ("vehUser", 
function (player) 
  
players [player] = {} 
  
end 
) 
  
addCommandHandler ("addVehicle", 
function (player, _, car) 
  
for name, v in pairs (players) do 
if (name == player) then 
table.insert (v.cars, car) 
  
end 
) 
  
addCommandHandler ("lockFor", 
function (player, _, otherPlayer) 
local otherPlayer = getPlayerFromName (otherPlayer) 
  
  
for name, v in pairs (players) do 
if (name == player) then 
table.insert (v.unlockedFor, otherPlayer) 
end 
end 
  
end 
) 
  
  

It looks a lot like this.

Now you can do the 'unlockCarFor' command, opposite of the one above.

and when a player enters the car as passager, you can loop through v.unlockedFor, see if there's a name similiar to the one that wants to enter, if yes let him enter, if no then cancelEvent

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