Jump to content

Advanced Lock System


shaio

Recommended Posts

I've been working on this code for like a week, I wanted to create a good lock system. It started out as just /lock, people would be able to jack your car if it wasn't locked, so I decided to make an auto 'owning' feature, if you spawn the car, you own the car and nobody can take it, however they can get in as a passenger, if you don't want anyone getting in your car at all you simply press K while in the vehicle. I plan to make a sharing feature, where you can allow other people to drive your car. Other than that, this is what I got.

If anyone has any suggestions for me, or if there is a way to make this script more efficient, just let me know.

My skype is cemour.burkoff

[Tested and functional]

function lock(player,cmd) 
    if getPedOccupiedVehicle(player) and getPedOccupiedVehicleSeat(player) == 0 then 
        if (getElementData(getPedOccupiedVehicle(player),"locks") == true) then 
            setElementData(getPedOccupiedVehicle(player),"lock",false) 
            setElementData(getPedOccupiedVehicle(player),"locks",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 
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 enterVehicle(player,seat,jacked) 
    if not (getElementData(source,"lock") == getPlayerSerial(player)) then 
        if (getElementData(source,"lock") == false) then 
            return 
        else 
            cancelEvent() 
            outputChatBox("This is not your vehicle!",player,0,255,255) 
        end 
    end 
end 
addEventHandler("onVehicleStartEnter",getRootElement(),enterVehicle) 
  
function inveh(player,seat,jacked) 
    if not (getElementData(source,"locked") == getPlayerSerial(player)) then 
        if seat == 0 then 
            cancelEvent() 
            outputChatBox("This is not your vehicle!",player,0,255,255) 
        else 
            return 
        end 
    else 
        return 
    end 
end 
addEventHandler("onVehicleStartEnter",getRootElement(),inveh) 
  
function lockVeh(player,seat,jacked) 
    setElementData(source,"locked",getPlayerSerial(player)) 
end 
addEventHandler("onVehicleEnter",getRootElement(),lockVeh) 

Link to comment

Bit more optimized and (May) be better suited for your attentions

    function lock(player,cmd) 
        if getPedOccupiedVehicle(player) and getPedOccupiedVehicleSeat(player) == 0 then 
            if (getElementData(getPedOccupiedVehicle(player),"locks") == true) then 
                setElementData(getPedOccupiedVehicle(player),"lock",false) 
                setElementData(getPedOccupiedVehicle(player),"locks",false) 
                outputChatBox("Unlocked!",player,0,255,255) 
            else 
                setElementData(getPedOccupiedVehicle(player),"lock",getAccountName(getPlayerAccount(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 
    addCommandHandler("lock",lock) 
      
    addEventHandler ("onPlayerJoin", root, 
    function() 
        bindKey (source, "k", "down", lock) 
    end) 
      
    addEventHandler ("onResourceStart", resourceRoot, 
    function() 
        local players = getElementsByType ("player") 
        for k, player in ipairs (players) do 
            bindKey (player, "k", "down", lock) 
        end 
    end) 
      
    function enterVehicle(player,seat,jacked) 
        if not (getElementData(source,"lock") == getAccountName(getPlayerAccount(player)) then 
            if (getElementData(source,"lock") == false) then 
                return 
            else 
                cancelEvent() 
                outputChatBox("This is not your vehicle!",player,0,255,255) 
            end 
        end 
    end 
    addEventHandler("onVehicleStartEnter",root,enterVehicle) 
      
    function inveh(player,seat,jacked) 
        if not (getElementData(source,"locked") == getAccountName(getPlayerAccount(player))) then 
            if seat == 0 then 
                cancelEvent() 
                outputChatBox("This is not your vehicle!",player,0,255,255) 
            end 
        end 
    end 
    addEventHandler("onVehicleStartEnter",root,inveh) 
      
    function lockVeh(player,seat,jacked) 
        setElementData(source,"locked",getAccountName(getPlayerAccount(player))) 
    end 
    addEventHandler("onVehicleEnter",root,lockVeh) 

Link to comment
Bit more optimized and (May) be better suited for your attentions

My server doesn't require a login, cuz I have a team selection when a player joins and the text always goes over the login screen, no matter what it is. I just assume go with serials rather than accounts, since some servers don't use accounts.

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