Jump to content

Need Help for a Vehicle lock/owner script


Rotti

Recommended Posts

Hello,

I want to make a script that can set an owner for a vehicle and only the owner can lock or unlock his car.

Here is what happens when someone buys a car:

function Perennial_buy () 
    local Perennial = createVehicle(404, -2600, 2268, -- s8) -->
    setElementData(Perennial, "save", "save")--for the save script 
    takePlayerMoney(source, 2000)   --take money 
    setTimer(warpPedIntoVehicle, 50, 1, source, Perennial)--warp player in vehicle 
    setElementData(Faggio, "owner", getPlayerName(source))--set the owner 
    outputServerLog("[AUTOHÄNDLER] "..getPlayerName(source).." kaufte einen Perennial")--server log 
end 

I don't know what that text in line 2 is but it is not in the script just in the forum and i can't delete it...

and thats the script for the lock

function lockcar(player) 
    posX, posY, posZ = getElementPosition(player) 
    local lockSphere = createColSphere( posX, posY, posZ, 20 ) 
    local locker = getPlayerName( player ) 
    local nearbyVehicles = getElementsWithinColShape( lockSphere, "vehicle" ) 
    destroyElement( lockSphere ) 
    for index, nearbyVehicle in ipairs( nearbyVehicles ) do 
        if getElementData(nearbyVehicle, "owner") == getPlayerName(source) then 
            setVehicleLocked(nearbyVehicle, true) 
        end 
    end 
end 
  
function binds() 
    bindKey(source, "L", "down", lockcar) 
end 
addEventHandler("onPlayerLogin", getRootElement(), binds) 
  
  

But it doesn't locks the car! I think it has to be something with the owner script because an outputChatBox before the "for" works! please help

Link to comment
function lockcar ( player ) 
    local posX, posY, posZ = getElementPosition ( player ) 
    local lockSphere = createColSphere ( posX, posY, posZ, 20 ) 
    local locker = getPlayerName ( player ) 
    local nearbyVehicles = getElementsWithinColShape ( lockSphere, 'vehicle' ) 
    destroyElement ( lockSphere ) 
    for index,nearbyVehicle in ipairs( nearbyVehicles ) do 
        if getElementData ( nearbyVehicle, 'owner') == getPlayerName ( player ) then 
            setVehicleLocked ( nearbyVehicle, true ) 
        end 
    end 
end 
  
function binds() 
    bindKey ( source, 'L', 'down', lockcar ) 
end 
addEventHandler ( 'onPlayerLogin', root, binds ) 

* Note : Using serial instead of name is much better. Because unlike serials, names are changable .

Link to comment

Its still the same problem. The vehicle doesn't locks when i press L. And i think it has to be something with the Owner, because the Vehicle locks if i take out this part:

if getElementData(nearbyVehicle, "owner") == getPlayerName(player) 
    setVehicleLocked(nearbyVehicle, true) 
        end 

Link to comment

I know. There is a forum bug so the code is not showing correctly !

function Perennial_buy () 
    local Perennial = createVehicle(404, -2600, 2268, -- s8) -->
    setElementData(Perennial, "save", "save")--for the save script 
    takePlayerMoney(source, 2000)   --take money 
    setTimer(warpPedIntoVehicle, 50, 1, source, Perennial)--warp player in vehicle 
    setElementData(, "owner", getPlayerName(source))--set the owner 
    outputServerLog("[AUTOHÄNDLER] "..getPlayerName(source).." kaufte einen Perennial")--server log 
end 

The bug is in line 2 as you can see . And the error is in setElementData at line 6 .

Link to comment
  • Moderators

You already have the user data and that is the player.

  
local vehicle = createVehicle ( 270, 0, 0, 0) -- create a vehicle, when you create an element it will returns a userdata. 
  
outputChatBox(tostring(vehicle)) 
  

A player does also have a user data.

  
addCommandHandler ( "myUserData",  
function ( player) 
    outputChatBox(tostring(player)) 
end) 

Edited by Guest
Link to comment
  • Moderators

yes, see my sample. (tostring is making sure that when I put it the chatbox, it won't give any errors. Userdata have to be a string to output.)

  
    function Perennial_buy () 
        local Perennial = createVehicle(404, -2600, 2268, -- s8) -->
        setElementData(Perennial, "save", "save")--for the save script 
        takePlayerMoney(source, 2000)   --take money 
        setTimer(warpPedIntoVehicle, 50, 1, source, Perennial)--warp player in vehicle 
        setElementData(source, "owner", source)--set the owner 
        outputServerLog("[AUTOHÄNDLER] "..getPlayerName(source).." kaufte einen Perennial")--server log 
    end 
  

  
function lockcar ( player ) 
    local posX, posY, posZ = getElementPosition ( player ) 
    local lockSphere = createColSphere ( posX, posY, posZ, 20 ) 
    for index,nearbyVehicle in pairs(getElementsWithinColShape ( lockSphere, 'vehicle' )) do -- just use pairs instead of ipairs  
        if getElementData ( nearbyVehicle, 'owner') == player then 
            setVehicleLocked ( nearbyVehicle, true ) 
        end 
    end 
    destroyElement ( lockSphere ) 
end 
  
addEventHandler ( 'onPlayerLogin', root,     
function () 
    bindKey ( source, 'L', 'down', lockcar ) 
end) 
  
  
  
  

BTW to many element data will start lagging, it is recommended to learn working with tables.

Like:

vehicleOwnGroup = {} 
vehicleOwnGroup[player]=  { , , , } 
  

Why is element data laggy? Because when you create it it will be send to all the players in the game.

(only client element data have an option that disallow the server to share it.)

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