Jump to content

Setting a name for the vehicle


myonlake

Recommended Posts

Hello,

I am just thinking that is it possible to make many cars via a command, then giving them all a different name. When I enter the car, I need to have the correct key for it.

I have tried elementData, but it seems impossible, because... I have only one createVehicle. I dunno.

function createVehicle(thePlayer, cmd, id) 
    local x, y, z = getElementPosition(thePlayer) 
     
    if id then 
        createVehicle(id, x, y, z) 
        setElementData(whathere, "server.key", math.random(0, 1000)) 
        setElementData(thePlayer, "server.key", getElementData(whathere, "server.key")) 
        outputChatBox(getElementData(whathere, "server.key") .. " is the ID of the vehicle.", thePlayer, 255, 255, 255, false) 
    else 
        outputChatBox("Syntax: /cv ", thePlayer, 220, 220, 0, false) 
    end 
end 
addCommandHandler("cv", createVehicle) 
  
function enterVehicle(thePlayer) 
    local theVehicle = getPedOccupiedVehicle(thePlayer) 
     
    if not getElementData(whathere, "server.key") == getElementData(thePlayer, "server.key") then 
        outputChatBox("Incorrect key", thePlayer, 255, 0, 0, false) 
    end 
end 
addEventHandler("onVehicleEnter", getRootElement(), enterVehicle) 
 
Edited by myonlake
Link to comment
What do you mean?

Creating multiple vehicles is with multiple createVehicle, preferably a loop.

Post your trial?

I am trying to do a vehicle key script. You spawn a vehicle, it has a key that you have too, if you don't have the same key, you won't be able to enter the vehicle, there is an example below, I didn't do the lock thing yet..

function createVehicle(thePlayer, cmd, id) 
    local x, y, z = getElementPosition(thePlayer) 
     
    if id then 
        createVehicle(id, x, y, z) 
        setElementData(theVehicle, "server.key", math.random(0, 1000)) 
        setElementData(thePlayer, "server.key", getElementData(whathere, "server.key")) 
        outputChatBox(getElementData(whathere, "server.key") .. " is the ID of the vehicle.", thePlayer, 255, 255, 255, false) 
    else 
        outputChatBox("Syntax: /cv ", thePlayer, 220, 220, 0, false) 
    end 
end 
addCommandHandler("cv", createVehicle) 
  
function enterVehicle(thePlayer) 
    local theVehicle = getPedOccupiedVehicle(thePlayer) 
     
    if not getElementData(theVehicle, "server.key") == getElementData(thePlayer, "server.key") then 
        outputChatBox("Incorrect key", thePlayer, 255, 0, 0, false) 
    end 
end 
addEventHandler("onVehicleEnter", getRootElement(), enterVehicle) 

Edited by Guest
Link to comment

Alright, I am doing this another way now.

I have a command that spawns a car, the command gives me a data with math.random. It works normally.

When I enter the car I spawned, it sets MY DATA into the car aswell, we both have identical DATA.

When someone else does the command too, it works normally. When he enters my car, he SHOULD get ejected from the vehicle, but... unfortunatly he doesn't. What's wrong?

function enterVehicle(thePlayer) 
    local theVehicle = getPedOccupiedVehicle(thePlayer) 
    local veh = getElementData(theVehicle, "sfserver.key") 
    local pla = getElementData(thePlayer, "sfserver.key") 
     
    if not getElementData(theVehicle, "sfserver.key") and getElementData(thePlayer, "sfserver.key") then 
        setElementData(theVehicle, "sfserver.key", getElementData(thePlayer, "sfserver.key")) 
        outputChatBox("Vehicle has now a key: " .. getElementData(thePlayer, "sfserver.key") .. ".", thePlayer, 0, 255, 0, false) 
     
    elseif getElementData(theVehicle, "sfserver.key") and not veh == pla or not getElementData(thePlayer, "sfserver.key") then 
        removePedFromVehicle(thePlayer) 
        outputChatBox("You have an incorrect key, it means you don't own this vehicle.", thePlayer, 0, 255, 0, false) 
    end 
end 
addEventHandler("onVehicleEnter", getRootElement(), enterVehicle) 

Link to comment
function enterVehicle(thePlayer) 
    local theVehicle = source 
    local veh = getElementData(theVehicle, "sfserver.key") 
    local pla = getElementData(thePlayer, "sfserver.key") 
    
    if not getElementData(theVehicle, "sfserver.key") and getElementData(thePlayer, "sfserver.key") then 
        setElementData(theVehicle, "sfserver.key", getElementData(thePlayer, "sfserver.key")) 
        outputChatBox("Vehicle has now a key: " .. getElementData(thePlayer, "sfserver.key") .. ".", thePlayer, 0, 255, 0, false) 
    elseif getElementData(theVehicle, "sfserver.key") and not veh == pla or not getElementData(thePlayer, "sfserver.key") then 
        removePedFromVehicle(thePlayer) 
        outputChatBox("You have an incorrect key, it means you don't own this vehicle.", thePlayer, 0, 255, 0, false) 
    end 
end 
addEventHandler("onVehicleEnter", getRootElement(), enterVehicle) 

Not sure if it'll work, as i couldn't test it.

P.S: The 'source 'of onVehicleEnter is the VEHICLE the player just entered.

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