Jump to content

[SCRIPT] Custom vehicle by user account [2 options]


knightscript

Recommended Posts

Hello, I´m sharing the script I´m using on my server (see signature) to spawn a vehicle, which would be ONLY accessible to the account you choose, so here we go,

First, if you are going to make this a resource and not included to the gamemode, then you want to create a folder, lets name our folder mycustomcar,

afterwards, we want to create 2 files:

* meta.xml

* car.lua

so your folder should look like this:

Screenshot_1.png

Afterwards, we open our meta.xml file and paste the following:

<meta> 
    <info author="YourNickname" type="script" name="My Custom car by user account" /> <!--This doesn´t really have to be on your script.--> 
    <script src="car.lua" type="server" /> <!--Defines where your script is, and that it is server-sided.--> 
</meta> 

So now, we are going to open our file named car.xml, and we insert the following code:

allowedaccount = "admin" --The account that is allowed to spawn the car 
function mycarbyuseracc(player) --create the function 
    local account = getAccountName(getPlayerAccount(player)) --get the player account 
    if account == allowedaccount then --if current player account matches allowed accounts, then do: 
       if (isElement(getElementData(player, "knightcar1"))) then --check if there is another vehicle that was spawned by the same command 
            destroyElement(getElementData(player, "knightcar1")) --destroy it 
        end --finalize what happens if there is another vehicle 
        outputChatBox("Welcome, "..account.."!.", player, 0, 255, 0, false) --displays to the player a text, you can remove this if you don´t want to show anything 
        outputChatBox("Here is your car! Have fun!", player, 0, 255, 0, false) --displays one more text to the player 
        local x,y,z = getElementPosition(player) --gets the X, Y and Z of the player and stores it in a variable 
        local rotX, rotY, rotZ = getElementRotation(player) -- gets the rotation of the player and stores it in a variable 
        local veh = createVehicle("602",x,y +-2,z +2) --creates the vehicle with id 602 (alpha), what you see at the end of z, makes the vehicle spawn a little bit over the ground, it can sometimes bug on the floor if not, see complete vehicle ID´s at [url=https://wiki.multitheftauto.com/wiki/Vehicle_IDs]https://wiki.multitheftauto.com/wiki/Vehicle_IDs[/url] 
        setElementRotation(veh,rotX,rotY,rotZ+10) --sets the vehicle rotation as the player rotation 
        setVehicleHeadLightColor (veh,255,0,0) --sets the vehicle lights color to red, (RGB) 
        setVehicleColor (veh,255,0,0) --sets the vehicle color to red, (RGB) 
--YOU CAN GENERATE COLORS ON THIS WEBSITE, [url=http://www.rapidtables.com/web/color/RGB_Color.htm]http://www.rapidtables.com/web/color/RGB_Color.htm[/url] 
        setElementData(player, "knightcar1", veh) --set the vehicles element to mycarmustang, if you changed the above element datas, then you must change thisone too, 
       -- warpPedIntoVehicle(player,veh) --this is if you want to warp the player inside the vehicle one it spawns 
      end 
end 
  addEventHandler( "onPlayerQuit", getRootElement( ),function() --when player quits... 
       if (isElement(getElementData(source, "knightcar1"))) then --if vehicle exists 
        destroyElement(getElementData(source, "knightcar1")) --destroy vehicle 
        --outputDebugString ( "1 car by Knight Destroyed." ) --just for debuging purposes 
        end 
  end) 

and you also must add the Command, lets say i want to spawn the car with the command /MyCarRed:

addCommandHandler("MyCarRed",mycarbyuseracc) 

Just for you to remember, commands are CASE SENSITIVE!.

So, when you type /MyCarRed, this would show up:

mta_screen_2016_01_20_08_23_49.png

When you type /MyCarRed for the second time, your old vehicle will disappear, if you dont want this to happen, just delete or comment out this lines (add "--" at the beggining of the line):

       if (isElement(getElementData(player, "mycarmustang"))) then 
            destroyElement(getElementData(player, "mycarmustang")) 
        end 

If you want to make your vehicle everything damageproof, add this:

setVehicleDamageProof(veh, true) --where true means yes, if you change true to false, it will disable this. 

EDIT: with this modifications, instead of having to edit the LUA script, you can edit it directly from your server

So lets start, first, open your meta.xml file, and paste the following:

<meta> 
    <info author="YourNickname" type="script" name="My Custom car by user account" /> <!--This doesn´t really have to be on your script.--> 
    <script src="car.lua" type="server" /> <!--Defines where your script is, and that it is server-sided.--> 
    <settings> 
        <setting name="*Account" value="admin" /> <!--Here you must define the account that can spawn the car --> 
    </settings> 
</meta> 

Afterwards, you are going to want to edit your car.lua file, and REPLACE with the following code:

function mycarbyuseracc(player) --create the function 
    local account = getAccountName(getPlayerAccount(player)) --get the player account 
    if account == get("Account") then --if current player account matches allowed account, then do: 
       if (isElement(getElementData(player, "mycar1"))) then --check if there is another vehicle that was spawned by the same command 
            destroyElement(getElementData(player, "mycar1")) --destroy it 
        end --finalize what happens if there is another vehicle 
        outputChatBox("Welcome, "..account.."!.", player, 0, 255, 0, false) --displays to the player a text, you can remove this if you don´t want to show anything 
        outputChatBox("Here is your car! Have fun!", player, 0, 255, 0, false) --displays one more text to the player 
        local x,y,z = getElementPosition(player) --gets the X, Y and Z of the player and stores it in a variable 
        local rotX, rotY, rotZ = getElementRotation(player) -- gets the rotation of the player and stores it in a variable 
        local veh = createVehicle("602",x,y +-2,z +2) --creates the vehicle with id 602 (alpha), what you see at the end of z, makes the vehicle spawn a little bit over the ground, it can sometimes bug on the floor if not, see complete vehicle ID´s at [url=https://wiki.multitheftauto.com/wiki/Vehicle_IDs]https://wiki.multitheftauto.com/wiki/Vehicle_IDs[/url] 
        setElementRotation(veh,rotX,rotY,rotZ+10) --sets the vehicle rotation as the player rotation 
        setVehicleHeadLightColor (veh,255,0,0) --sets the vehicle lights color to red, (RGB) 
        setVehicleColor (veh,255,0,0) --sets the vehicle color to red, (RGB) 
--YOU CAN GENERATE COLORS ON THIS WEBSITE, [url=http://www.rapidtables.com/web/color/RGB_Color.htm]http://www.rapidtables.com/web/color/RGB_Color.htm[/url] 
        setElementData(player, "mycar1", veh) --set the vehicles element to mycarmustang, if you changed the above element datas, then you must change thisone too, 
       -- warpPedIntoVehicle(player,veh) --this is if you want to warp the player inside the vehicle one it spawns 
      end 
end 

And the command:

addCommandHandler("mycar",mycarbyuseracc) --command would be /mycar 

So, you can edit the account that can use the vehicle by 2 methods:

1.) By editing the XML: You can edit the XML and edit value="admin" to the account name you want to use, for example:

<!--<setting name="*Account" value="admin" />--> <!--OLD ACCOUNT--> 
<setting name="*Account" value="myaccount" /> <!--MY NEW ACCOUNT--> 

2.) By editing the option on your admin panel: For this, you will need to have the resource ADMIN started on your server, afterwards, press letter P to start your admin panel, you will see something like this:

Untitled.png

You will want to go to the tab named Resources, and you will see a searchbox, in which you will type in the name of the folder you created, remember to move your folder to the server´s resources folder, once you search for your folder´s name, you will see something like this:

Untitled.png

By double clicking it, you will see something like this:

Untitled.png

Once you are here, you want to double click the field "Account", and you will see a textbox like thisone:

mta_screen_2016_01_20_10_58_41.png

Just replace the content with the account name you want, and click OK, then restart the resource by clicking "Restart", you can see it located here:

Untitled.png

If your resource shows up like "loaded", click on Start, if your resource shows up like "running", click on Restart.

So that would be it!

Thanks for visiting my post!

Edited by Guest
Link to comment
  • 9 months later...

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