Jump to content

Need Help for a Vehicle lock/owner script


Rotti

Recommended Posts

Posted

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

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

Posted

I've found one mistake:

at setElementData in the buy script the :o but it didn't help so far...

But I've corrected it

Posted
I've found one mistake:

at setElementData in the buy script the :o but it didn't help so far...

But I've corrected it

I don't get it :S

Did it work or you still have problems ?

Posted

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 

Posted

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 .

Posted

do you mean this text with img src and so on?

thats only on this website not in the script and i can't delete it...

  • Moderators
Posted

You should not store the player name or the serial, just use the player userdata.

Elements can't have the same userdata..... -_-"

  • Moderators
Posted (edited)

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
  • Moderators
Posted

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

Posted

Thank you for your help it worked but if i reconnect i can't lock the vehicle again...

So if the vehicle is unlocked, i reconnect and try to lock it it doesn't locks

I hope that you understand it :D

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