Jump to content

Help with vehicle parking script


Recommended Posts

Hey guys. this script is perfect except for that it wont remember the players name. Essentially what i want it to do is when the player sends the command it parks the car and remembers who parks it. if the person who parked it gets back in then it should start automatically and if it isnt then it expels the player from the car. the problem is that I park the car and when i get back in it expels me. how can i get it to remember me?

function publicPark ( thePlayer ) 
     playervehicle = getPlayerOccupiedVehicle ( thePlayer ) 
     playername = getVehicleOccupant ( playervehicle, 0 ) 
     id = getVehicleID ( playervehicle ) 
     if ( playervehicle ) then 
          if id == 444 or id == 556 or id == 557 or id == 406 then 
               setVehicleFrozen ( playervehicle, true ) 
               setVehicleDamageProof ( playervehicle, true ) 
               setVehicleOverrideLights ( playervehicle, 1 ) 
               setVehicleEngineState ( playervehicle, false ) 
               authorized = getClientName ( playername ) 
               outputChatBox ( "Vehicle Parked", thePlayer, 255, 0, 0 ) 
          else 
               if ( playervehicle ) then 
                    if isVehicleOnGround ( playervehicle ) then 
                         setVehicleFrozen ( playervehicle, true ) 
                         setVehicleDamageProof ( playervehicle, true ) 
                         setVehicleOverrideLights ( playervehicle, 1 ) 
                         setVehicleEngineState ( playervehicle, false ) 
                         outputChatBox ( "Vehicle Parked", thePlayer, 255, 0, 0 )    
                    end 
               end 
           end 
      end 
end 
  
function playerExpel ( thePlayer ) 
      
          if ( authorized ) then 
               setVehicleFrozen ( playervehicle, false ) 
               setVehicleDamageProof ( playervehicle, false ) 
               setVehicleOverrideLights ( playervehicle, 0 ) 
               setVehicleEngineState ( playervehicle, true ) 
               outputChatBox ( "Vehicle Started", thePlayer, 255, 0, 0 ) 
          else 
               removePlayerFromVehicle ( thePlayer ) 
               outputChatBox ( "This is not your vehicle!", thePlayer, 255, 0, 0 ) 
          end 
      
end 
  
addCommandHandler ( "parkcar", publicPark ) 
addEventHandler ( "onVehicleEnter", getRootElement(), playerExpel ) 
      
           

Edited by Guest
Link to comment

You're setting it as a global variable, so if multiple people try to park, then you're going to get mixed results about who is the variable 'authorised'. Try pulling the data and having it save to an external file, and then when you need it, pull it from the file again and run a check on it.

Link to comment

that kind of works. if i enter any car it expels me, if i give myself a car with admin CP and get out and get back it it expels me. however if i give myself a car with CP and park it, get out then back in it starts for me. any soltions?

function publicPark ( thePlayer ) 
     playervehicle = getPlayerOccupiedVehicle ( thePlayer ) 
     playername = getVehicleOccupant ( playervehicle, 0 ) 
     id = getVehicleID ( playervehicle ) 
     if ( playervehicle ) then 
          if id == 444 or id == 556 or id == 557 or id == 406 then 
               setVehicleFrozen ( playervehicle, true ) 
               setVehicleDamageProof ( playervehicle, true ) 
               setVehicleOverrideLights ( playervehicle, 1 ) 
               setVehicleEngineState ( playervehicle, false ) 
               authorized = getClientName ( playername ) 
               setElementData ( playervehicle, "parkedby", playername ) 
               outputChatBox ( "Vehicle Parked", thePlayer, 255, 0, 0 ) 
          else 
               if ( playervehicle ) then 
                    if isVehicleOnGround ( playervehicle ) then 
                         setVehicleFrozen ( playervehicle, true ) 
                         setVehicleDamageProof ( playervehicle, true ) 
                         setVehicleOverrideLights ( playervehicle, 1 ) 
                         setVehicleEngineState ( playervehicle, false ) 
                         setElementData ( playervehicle, "parkedby", playername ) 
                         outputChatBox ( "Vehicle Parked", thePlayer, 255, 0, 0 )    
                    end 
               end 
           end 
      end 
end 
  
function playerExpel ( thePlayer ) 
      
          if ( getElementData ( playervehicle, "parkedby" ) == playername ) then 
               setVehicleFrozen ( playervehicle, false ) 
               setVehicleDamageProof ( playervehicle, false ) 
               setVehicleOverrideLights ( playervehicle, 0 ) 
               setVehicleEngineState ( playervehicle, true ) 
               outputChatBox ( "Vehicle Started", thePlayer, 255, 0, 0 ) 
          else 
               removePlayerFromVehicle ( thePlayer ) 
               outputChatBox ( "This is not your vehicle!", thePlayer, 255, 0, 0 ) 
          end 
      
end 
  
addCommandHandler ( "parkcar", publicPark ) 
addEventHandler ( "onVehicleEnter", getRootElement(), playerExpel ) 
      
           

I get errors for the elementData stuff. i dont have much faith for this script anymore if i cant figure something out soon

Edited by Guest
Link to comment
  
function playerExpel ( thePlayer ) 
      
          if ( getElementData ( playervehicle, "parkedby" ) == playername ) then 
               setVehicleFrozen ( playervehicle, false ) 
               setVehicleDamageProof ( playervehicle, false ) 
               setVehicleOverrideLights ( playervehicle, 0 ) 
               setVehicleEngineState ( playervehicle, true ) 
               outputChatBox ( "Vehicle Started", thePlayer, 255, 0, 0 ) 
          else 
               removePlayerFromVehicle ( thePlayer ) 
               outputChatBox ( "This is not your vehicle!", thePlayer, 255, 0, 0 ) 
          end 
      
end 
  

In this function playervehicle and playername are undefined - you determine them in the other function but not in this one. You're also saving the owner of a vehicle as a player element type but call the variable "playername" which is confusing. And what is the "authorized" variable ? That's the name of the player but you don't use it. In fact, I wouldn't use the name (you're not) as it could change. Edit: I notice that you've made the variables global - that's never going to work in the long run when you've multiple people on the server.

  
function playerExpel ( thePlayer ) 
           
          playervehicle = getPlayerOccupiedVehicle ( thePlayer ) 
      
          if ( getElementData ( playervehicle, "parkedby" ) == thePlayer ) then 
               setVehicleFrozen ( playervehicle, false ) 
               setVehicleDamageProof ( playervehicle, false ) 
               setVehicleOverrideLights ( playervehicle, 0 ) 
               setVehicleEngineState ( playervehicle, true ) 
               outputChatBox ( "Vehicle Started", thePlayer, 255, 0, 0 ) 
          else 
               removePlayerFromVehicle ( thePlayer ) 
               outputChatBox ( "This is not your vehicle!", thePlayer, 255, 0, 0 ) 
          end 
      
end 
  

Link to comment
  
function playerExpel ( thePlayer ) 
      
          if ( getElementData ( playervehicle, "parkedby" ) == playername ) then 
               setVehicleFrozen ( playervehicle, false ) 
               setVehicleDamageProof ( playervehicle, false ) 
               setVehicleOverrideLights ( playervehicle, 0 ) 
               setVehicleEngineState ( playervehicle, true ) 
               outputChatBox ( "Vehicle Started", thePlayer, 255, 0, 0 ) 
          else 
               removePlayerFromVehicle ( thePlayer ) 
               outputChatBox ( "This is not your vehicle!", thePlayer, 255, 0, 0 ) 
          end 
      
end 
  

In this function playervehicle and playername are undefined - you determine them in the other function but not in this one. You're also saving the owner of a vehicle as a player element type but call the variable "playername" which is confusing. And what is the "authorized" variable ? That's the name of the player but you don't use it. In fact, I wouldn't use the name (you're not) as it could change. Edit: I notice that you've made the variables global - that's never going to work in the long run when you've multiple people on the server.

  
function playerExpel ( thePlayer ) 
           
          playervehicle = getPlayerOccupiedVehicle ( thePlayer ) 
      
          if ( getElementData ( playervehicle, "parkedby" ) == thePlayer ) then 
               setVehicleFrozen ( playervehicle, false ) 
               setVehicleDamageProof ( playervehicle, false ) 
               setVehicleOverrideLights ( playervehicle, 0 ) 
               setVehicleEngineState ( playervehicle, true ) 
               outputChatBox ( "Vehicle Started", thePlayer, 255, 0, 0 ) 
          else 
               removePlayerFromVehicle ( thePlayer ) 
               outputChatBox ( "This is not your vehicle!", thePlayer, 255, 0, 0 ) 
          end 
      
end 
  

Tested your code and when I spawn a vehicle it says it isn't myne when it was nobodys...

Link to comment

ugh, a different error now. it ways INFO: loading script failed park.lua:11: ")" expected near ","

function publicPark ( thePlayer ) 
     playervehicle = getPlayerOccupiedVehicle ( thePlayer ) 
     playername = getVehicleOccupant ( playervehicle, 0 ) 
     id = getVehicleID ( playervehicle ) 
     if ( playervehicle ) then 
          if id == 444 or id == 556 or id == 557 or id == 406 or id == 472 or id == 473 or id == 493 or id == 595 or id == 484 or id == 430 or id == 453 or id == 452 or id == 446 or id == 454 then 
               setVehicleFrozen ( playervehicle, true ) 
               setVehicleDamageProof ( playervehicle, true ) 
               setVehicleOverrideLights ( playervehicle, 1 ) 
               setVehicleEngineState ( playervehicle, false ) 
               local setElementData ( playervehicle, "parkedby", playername ) 
               outputChatBox ( "Vehicle Parked", thePlayer, 255, 0, 0 ) 
          else 
               if ( playervehicle ) then 
                    if isVehicleOnGround ( playervehicle ) then 
                         setVehicleFrozen ( playervehicle, true ) 
                         setVehicleDamageProof ( playervehicle, true ) 
                         setVehicleOverrideLights ( playervehicle, 1 ) 
                         setVehicleEngineState ( playervehicle, false ) 
                         local setElementData ( playervehicle, "parkedby", playername ) 
                         outputChatBox ( "Vehicle Parked", thePlayer, 255, 0, 0 )    
                    end 
               end 
           end 
      end 
end 
  
function playerExpel ( thePlayer ) 
     playervehicle = getPlayerOccupiedVehicle ( thePlayer ) 
     playername = getVehicleOccupant ( playervehicle, 0 ) 
     if isVehicleFrozen ( playervehicle ) then 
          if ( local getElementData ( playervehicle, "parkedby" ) == playername ) then 
               setVehicleFrozen ( playervehicle, false ) 
               setVehicleDamageProof ( playervehicle, false ) 
               setVehicleOverrideLights ( playervehicle, 0 ) 
               setVehicleEngineState ( playervehicle, true ) 
               outputChatBox ( "Vehicle Started", thePlayer, 255, 0, 0 ) 
          else 
               removePlayerFromVehicle ( thePlayer ) 
               outputChatBox ( "This is not your vehicle!", thePlayer, 255, 0, 0 ) 
          end 
     end 
end 
  
addCommandHandler ( "parkcar", publicPark ) 
addEventHandler ( "onVehicleEnter", getRootElement(), playerExpel ) 
      
           

Edited by Guest
Link to comment
ugh, a different error now. it ways INFO: loading script failed park.lua:11: ")" expected near ","
function publicPark ( thePlayer ) 
     playervehicle = getPlayerOccupiedVehicle ( thePlayer ) 
     playername = getVehicleOccupant ( playervehicle, 0 ) 
     id = getVehicleID ( playervehicle ) 
     if ( playervehicle ) then 
          if id == 444 or id == 556 or id == 557 or id == 406 or id == 472 or id == 473 or id == 493 or id == 595 or id == 484 or id == 430 or id == 453 or id == 452 or id == 446 or id == 454 then 
               setVehicleFrozen ( playervehicle, true ) 
               setVehicleDamageProof ( playervehicle, true ) 
               setVehicleOverrideLights ( playervehicle, 1 ) 
               setVehicleEngineState ( playervehicle, false ) 
               local setElementData ( playervehicle, "parkedby", playername ) 
               outputChatBox ( "Vehicle Parked", thePlayer, 255, 0, 0 ) 
          else 
               if ( playervehicle ) then 
                    if isVehicleOnGround ( playervehicle ) then 
                         setVehicleFrozen ( playervehicle, true ) 
                         setVehicleDamageProof ( playervehicle, true ) 
                         setVehicleOverrideLights ( playervehicle, 1 ) 
                         setVehicleEngineState ( playervehicle, false ) 
                         local setElementData ( playervehicle, "parkedby", playername ) 
                         outputChatBox ( "Vehicle Parked", thePlayer, 255, 0, 0 )    
                    end 
               end 
           end 
      end 
end 
  
function playerExpel ( thePlayer ) 
     playervehicle = getPlayerOccupiedVehicle ( thePlayer ) 
     playername = getVehicleOccupant ( playervehicle, 0 ) 
     if isVehicleFrozen ( playervehicle ) then 
          if ( local getElementData ( playervehicle, "parkedby" ) == playername ) then 
               setVehicleFrozen ( playervehicle, false ) 
               setVehicleDamageProof ( playervehicle, false ) 
               setVehicleOverrideLights ( playervehicle, 0 ) 
               setVehicleEngineState ( playervehicle, true ) 
               outputChatBox ( "Vehicle Started", thePlayer, 255, 0, 0 ) 
          else 
               removePlayerFromVehicle ( thePlayer ) 
               outputChatBox ( "This is not your vehicle!", thePlayer, 255, 0, 0 ) 
          end 
     end 
end 
  
addCommandHandler ( "parkcar", publicPark ) 
addEventHandler ( "onVehicleEnter", getRootElement(), playerExpel ) 
      
           

Line 11, remove local and just leave setElementData

Link to comment

at first it says it needs a parenthese around setelementdata. i got rid of local then it said that if was not supposed to be there. i got rid of if and it said == was not supposed to be there. by then i got pissed and gave up. its like when i try to define a colSphere in another script. i do it like the wiki says and it doesnt work

Link to comment

Look here:

ugh, a different error now. it ways INFO: loading script failed park.lua:11: ")" expected near ","
function publicPark ( thePlayer ) 
     playervehicle = getPlayerOccupiedVehicle ( thePlayer ) 
     playername = getVehicleOccupant ( playervehicle, 0 ) 
end 
  

You want to get "player" element and assign it to the playername variable. What do you need player element for if thePlayer is already what you need?

Anyways, I'll have a deeper look into it if you'll want to give up an I'll have some time.

Link to comment

well, it seems that it would make sense to do that, but it was giving me errors with basic things like "id == 123" which works on other stuff i make. this is no issue however, i already made a vehicle parking script in the past but i was just trying to make it more user friendly and let everyone use it. the current version doesnt work on boats and vehicles with big wheels like the monster ( game doesnt see them as on the ground ) and all the functions are command based therefore it is for admins only ( for things like parking a dangerous vehicle in area 69 ). you can look into it if you feel like it. im just saying dont feel obligated to fix my problems if you dont have to.

Link to comment

I'm not saying this will work but try this.....

Replace

addEventHandler ( "onVehicleEnter", getRootElement(), playerExpel ) 

with this:

addCommandHandler ( "unpark", playerExpel ) 

And something to add so no one can destroy it when its parked add this with your vehicle park functions:

setVehicleDamageProof ( theVehicle, true ) 

Then add this one on your vehicle started function:

setVehicleDamageProof ( theVehicle, false ) 

Link to comment
  • 2 years later...
Hey, I know last post was very old but, does anybody know if this script is working ?

For now, theres nothing like this... :/

not sure if he ever got it to work, so heres a basic one for you.

it will allow players to /lock cars...

local vehicleOwner = {} 
  
function lockCar( thePlayer ) 
  
    local vehicle =  getPlayerOccupiedVehicle( thePlayer ) 
    local vehicleID = getVehicleID( vehicle )    
    if vehicle then 
        if getVehicleOccupant( vehicle,0 ) == thePlayer then 
            vehicleOwner[vehicle] = getPlayerName(thePlayer) 
            setVehicleEngineState ( vehicle, false ) 
            outputChatBox( "Vehicle Parked And Locked", thePlayer, 255, 0, 0 ) 
        else 
            outputChatBox("You need to be the driver!",thePlayer,255,0,0) 
        end 
    else 
        outputChatBox("You need to be in a vehicle!",thePlayer,255,0,0) 
    end 
end 
addCommandHandler ( "lock", lockCar ) 
  
function ownerCheck( thePlayer ) 
  
    local playerName = getPlayerName(thePlayer) 
    local owner = vehicleOwner[source] 
    if owner == playerName then 
        setVehicleEngineState ( source, true ) 
        outputChatBox( "Vehicle Unlocked And Started", thePlayer, 0, 255, 0 ) 
        vehicleOwner[source] = nil -- < remove the current owner  
    end 
end 
addEventHandler( "onVehicleEnter", getRootElement(), ownerCheck ) 
  
function expelCheck( thePlayer ) 
  
    local playerName = getPlayerName(thePlayer) 
    local owner = vehicleOwner[source] 
    if owner then 
        if owner ~= playerName then 
            removePlayerFromVehicle( thePlayer ) 
            outputChatBox( "Vehicle Locked, Current Owner: " .. tostring(vehicleOwner[source]), thePlayer, 255, 0, 0 ) 
        end      
    end 
end 
addEventHandler( "onVehicleStartEnter", getRootElement(), expelCheck ) 

restarting the script will unlock all the cars and remove current owners.

Edited by Guest
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...