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 .

" Keep Thinking Different . " - Steve Jops

--------------------

Don't send me PMs asking for help, I Won't reply !

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 ?

" Keep Thinking Different . " - Steve Jops

--------------------

Don't send me PMs asking for help, I Won't reply !

Posted

Tell us what are they and give us the part ( from your code ) where you have problems .

" Keep Thinking Different . " - Steve Jops

--------------------

Don't send me PMs asking for help, I Won't reply !

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

Post the code when someone buys a car in pastebin website .

" Keep Thinking Different . " - Steve Jops

--------------------

Don't send me PMs asking for help, I Won't reply !

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 .

" Keep Thinking Different . " - Steve Jops

--------------------

Don't send me PMs asking for help, I Won't reply !

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

Posted

I know, It's a bug in the forum xD.

+ As i said :

the error is in setElementData at line 6

" Keep Thinking Different . " - Steve Jops

--------------------

Don't send me PMs asking for help, I Won't reply !

  • Moderators
Posted

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

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

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

  • 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

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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

  • Moderators
Posted

yes, that is true, it is because when you reconnect your userdata got changed.

To safe this per-ma, you have to use account data(mysql lite) or mysql.

Account data is easier, my mysql gives more performance at bigger data bases, but is harder to script.

https://wiki.multitheftauto.com/wiki/SetAccountData

https://wiki.multitheftauto.com/wiki/GetAccountData

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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