Jump to content

Check whether a player has a driving licence


Recommended Posts

Hi,

i made a little script where someone can buy a driving licence for 1200$, and it works like a charm. If the player hasn't got one yet, a new file with the player's name will be created, and you can always check if a player has a driving licence, by checking whether there's a file with his name or not.

Now, everytime a player enters a vehicle I want to check if the player has a licence. If not, the player will be forced out of the vehicle, and a text shows up in the chatbox. If yes, nothing will happen and the player is able to continue driving. But my script doesn't work. If I enter a vehicle with no driving licence, I can still drive it without any issues. Also, can I force the player to get out of the vehicle without removePedFromVehicle? I mean, like he then just leaves the vehicle, as if he pressed F.

First, if it matters, the working script for the licence:

  
desk = createObject( 2165, -1952.6999511719, 293.79998779297, 34.5, 0, 0, 90 ) 
ped = createPed( 69, -1952, 294, 35.5, 89.9986267, false ) 
  
function wangcars( playerSource ) 
    local x, y, z = getElementPosition( playerSource ) 
    local px, py, pz = getElementPosition( ped ) 
    local dist = getDistanceBetweenPoints3D( x, y, z, px, py, pz ) 
    local pedhealth = getElementHealth( ped ) 
    if ( dist < 3 ) and not ( isPedDead( ped ) ) then 
        local playername = getPlayerName( playerSource ) 
        local playermoney = getPlayerMoney( playerSource ) 
        if ( fileExists( "licences/"..playername..".txt" ) ) then 
            outputChatBox( "You already bought a driving licence.", playerSource, 255, 0, 0 ) 
        elseif ( playermoney < 1200 ) then 
            outputChatBox( "You don't have enough money. A driving licence costs 1200$!", playerSource, 255, 0, 0 ) 
        else 
            outputChatBox( "You have successfully bought a driving licence for 1200$. #BD0000Type /dellicence to delete your driving licence.", playerSource, 28, 189, 0, true ) 
            local fileHandle = fileCreate( "licences/"..playername..".txt" ) 
            setPlayerMoney( getPlayerFromName( playername ), playermoney - 1200 ) 
            if ( fileHandle ) then 
            fileWrite( fileHandle, "--Bought a driving licence--" ) 
            fileClose( fileHandle ) 
            end 
        end 
    else 
        outputChatBox( "Since you or someone else killed the trader, you can't buy a driving licence. Ask an administrator for help.", playerSource, 255, 0, 0 ) 
        return 
    end 
end 
addCommandHandler( "buylicence", wangcars ) 
  
--the player also has the ability to delete his licence: 
function dellicence( playerSource ) 
        local playername = getPlayerName( playerSource ) 
        if ( fileExists( "licences/"..playername..".txt" ) ) then 
            fileDelete( "licences/"..playername..".txt" ) 
            outputChatBox( "You deleted your driving licence.", playerSource, 0, 255, 0 ) 
        else 
            outputChatBox( "You don't have a driving licence!", playerSource, 255, 0, 0 ) 
        end  
end 
addCommandHandler( "dellicence", dellicence ) 
  

and the non-working script, to check if a player has a driving licence to get him out of a vehicle:

  
function havelicence( playerSource ) 
    local veh = getPedOccupiedVehicle( playerSource ) 
    if ( veh ) then 
        local vehseat = getPedOccupiedVehicleSeat( playerSource ) 
        if ( vehseat = 0 ) then 
            if not( fileExists( "licences/"..playername..".txt" ) ) then 
                removePedFromVehicle( playerSource ) 
                outputChatBox( "You do not have a driving licence. Buy one at Wang Cars for 1200$!", 255, 0, 0 ) 
                return 
            else  
                return 
            end 
        else 
            return 
        end 
    end 
end 
addEventHandler( "onPlayerVehicleEnter", getRootElement(), havelicence ) 
  

I would appreciate some help :P

Link to comment

For making / removing licences --

      
    desk = createObject( 2165, -1952.6999511719, 293.79998779297, 34.5, 0, 0, 90 ) 
    ped = createPed( 69, -1952, 294, 35.5, 89.9986267, false ) 
      
    function wangcars( playerSource ) 
        local x, y, z = getElementPosition( playerSource ) 
        local px, py, pz = getElementPosition( ped ) 
        local dist = getDistanceBetweenPoints3D( x, y, z, px, py, pz ) 
        local pedhealth = getElementHealth( ped ) 
        if ( dist < 3 ) and not ( isPedDead( ped ) ) then 
            local playername = getAccountName(getPlayerAccount(playerSource)) 
            local playermoney = getPlayerMoney( playerSource ) 
            if ( fileExists( "licences/"..playername..".txt" ) ) then 
                outputChatBox( "You already bought a driving licence.", playerSource, 255, 0, 0 ) 
            elseif ( playermoney < 1200 ) then 
                outputChatBox( "You don't have enough money. A driving licence costs 1200$!", playerSource, 255, 0, 0 ) 
            else 
                outputChatBox( "You have successfully bought a driving licence for 1200$. #BD0000Type /dellicence to delete your driving licence.", playerSource, 28, 189, 0, true ) 
                local fileHandle = fileCreate( "licences/"..playername..".txt" ) 
                setPlayerMoney( getPlayerFromName( playername ), playermoney - 1200 ) 
                if ( fileHandle ) then 
                fileWrite( fileHandle, "--Bought a driving licence--" ) 
                fileClose( fileHandle ) 
                end 
            end 
        else 
            outputChatBox( "Since you or someone else killed the trader, you can't buy a driving licence. Ask an administrator for help.", playerSource, 255, 0, 0 ) 
            return 
        end 
    end 
    addCommandHandler( "buylicence", wangcars ) 
      
    --the player also has the ability to delete his licence: 
    function dellicence( playerSource ) 
             local playername = getAccountName(getPlayerAccount(playerSource)) 
            if ( fileExists( "licences/"..playername..".txt" ) ) then 
                fileDelete( "licences/"..playername..".txt" ) 
                outputChatBox( "You deleted your driving licence.", playerSource, 0, 255, 0 ) 
            else 
                outputChatBox( "You don't have a driving licence!", playerSource, 255, 0, 0 ) 
            end  
    end 
    addCommandHandler( "dellicence", dellicence ) 
      

For handling if he/she has one --

      
         
    function havelicence() 
        local playerSource = source 
        local veh = getPedOccupiedVehicle( playerSource ) 
        if ( veh ) then 
            local vehseat = getPedOccupiedVehicleSeat( playerSource ) 
            if ( vehseat = 0 ) then 
                if not( fileExists( "licences/"..getAccountName(getPlayerAccount(playerSource))..".txt" ) ) then 
                    setControlState ( playerSource, "enter_exit", true ) 
                    setControlState ( playerSource, "enter_exit", false ) 
                    outputChatBox( "You do not have a driving licence. Buy one at Wang Cars for 1200$!",playerSource, 255, 0, 0 ) 
                end 
            end 
        end 
    end 
    addEventHandler( "onPlayerVehicleEnter", root, havelicence ) 
      

playerName was not defined in the kick player from vehicle thing. And getAccountName is a much better way of doing it.

Also, onPlayerVehicleEnter uses source instead of it being defined up top.

Link to comment

So in the first you just changed, that it will get the account?

And In the second, you also changed, that it will get the account, instead of the name? And you changed getRootElement() to root? 1. Did you change something else? 2. Why root instead of getRootElement()?

Note: in the buy licence script there is a pedhealth variable, which wasn't meant to be there ;D I first tried to look whether pedhealth >= 1 :D so you can ignore the variable.

Link to comment

Much better than your code try it (untested i'm using the phone).

local desk = createObject( 2165, -1952.6999511719, 293.79998779297, 34.5, 0, 0, 90 ) 
local ped = createPed( 69, -1952, 294, 35.5, 89.9986267, false ) 
  
function wangcars( playerSource ) 
    local x, y, z = getElementPosition( playerSource ) 
    local px, py, pz = getElementPosition( ped ) 
    local dist = getDistanceBetweenPoints3D( x, y, z, px, py, pz ) 
    local pedhealth = getElementHealth( ped ) 
    if ( dist < 3 ) and not ( isPedDead( ped ) ) then 
        local account = getPlayerAccount(playerSource) 
        if account and not isGuestAccount(account) then 
        local playermoney = getPlayerMoney( playerSource ) 
        local licence = getAccountData(account,"licence") 
             
        -- check if the player already have a driving licence 
        if licence then  
            outputChatBox( "You already bought a driving licence.", playerSource, 255, 0, 0 ) 
            return  
        end  
             
        -- check the player money 
        if ( playermoney < 1200 ) then 
            outputChatBox( "You don't have enough money. A driving licence costs 1200$!", playerSource, 255, 0, 0 ) 
            return  
        end  
         
        outputChatBox( "You have successfully bought a driving licence for 1200$. #BD0000Type /dellicence to delete your driving licence.", playerSource, 28, 189, 0, true ) 
        takePlayerMoney (playerSource, 1200) 
        setAccountData(account,"licence",true) 
      end  
    end 
end 
addCommandHandler( "buylicence", wangcars ) 
  
--the player also has the ability to delete his licence: 
function dellicence( playerSource ) 
    local account = getPlayerAccount(playerSource) 
    if account and not isGuestAccount(account) then 
        local playername = getPlayerName( playerSource ) 
        local licence = getAccountData(account,"licence") 
        if licence then  
            setAccountData(account,"licence",false) 
        end  
    end  
end 
addCommandHandler( "dellicence", dellicence ) 
  
  
  
function havelicence(playerSource,seat) 
    if seat == 0 then  
        local account = getPlayerAccount(playerSource) 
        if account and not isGuestAccount(account) then  
            local licence = getAccountData(account,"licence") 
            if not licence then  
                outputChatBox( "You do not have a driving licence. Buy one at Wang Cars for 1200$!",playerSource, 255, 0, 0 ) 
                cancelEvent() 
            end  
        end  
    end 
end 
addEventHandler("onVehicleStartEnter", getRootElement(), havelicence ) 

Edited by Guest
Link to comment

Thanks. The console says, that you forgot the "then" in line 12 ;)

And the begin bracket in line 28 :D

And you forgot to end the first function :)

and your script overall isn't working thank you :)

you can still enter any vehicle without driving licence, and when you bought your driving licence (now you can buy unlimited xD) you can't delete it anymore...

EDIT: okay ill try again

Edited by Guest
Link to comment
Thanks. The console says, that you forgot the "then" in line 12 ;)

And the begin bracket in line 28 :D

And you forgot to end the first function :)

and your script overall isn't working thank you :)

you can still enter any vehicle without driving licence, and when you bought your driving licence (now you can buy unlimited xD) you can't delete it anymore...

:fp: i said check my code , because i'm using the computer now. i edited my code 4 munites ago.

Link to comment

I fixed it by doing "if not" in line 7 :D now its working thanks!

EDIT: Ah I see you changed it too :)

  
function havelicence(playerSource,seat) 
    if seat == 0 then 
        local account = getPlayerAccount(playerSource) 
        if account and not isGuestAccount(account) then 
            local licence = getAccountData(account,"licence") 
            if not licence then 
                outputChatBox( "You do not have a driving licence. Buy one at Wang Cars for 1200$!",playerSource, 255, 0, 0 ) 
                cancelEvent() 
            end 
        end 
    end 
end 
addEventHandler("onVehicleStartEnter", getRootElement(), havelicence ) 
  

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