[RELEASE] Vehicle System

Started by MRSK143, Jun 10, 2022, 12:03 PM

Previous topic - Next topic

MRSK143

 ^-^Hi buddies, Today I'm here with a vehicle system, A simple vehicle system (bug-free).
Let me introduce to the commands these 4 commands use to add vehicles in database /addcar | /insertcar | /addveh | /insertveh
This command /removecar removes a car from the database but you must be in a car to remove the car from the database
And these 4 commands /editveh | /editcar | /updateveh | /updatecar help you to modify car data you can change the car model, world, position, colour 1 and colour 2

Put these database detail on script load event

[noae]Vehicles <- ConnectSQL("Vehicles.db");
QuerySQL(Vehicles, "CREATE TABLE IF NOT EXISTS Vehicles(ID NUMERIC DEFAULT 0, Model NUMERIC DEFAULT 0, World NUMERIC DEFAULT 0, Pos_X FLOAT, Pos_Y FLOAT, Pos_Z FLOAT, Angle FLOAT, Colour_1 NUMERIC DEFAULT 0, Colour_2 NUMERIC DEFAULT 0)");
LoadVehicles();
[/noae]

Commands are here just paste them into their right place

[noae]if(cmd == "addcar" || cmd == "insertcar" || cmd == "addveh" || cmd == "insertveh")
{
if(player.Name == "MR_SK") //you can edit this line as your wish
{
if(text)
{
if(player.IsSpawned)
{
local model = GetTok(text, " ", 1), world = GetTok(text, " ", 2), col1 = GetTok(text, " ", 3), col2 = GetTok(text, " ", 4);
if(model)
{
if(world)
{
if(col1)
{
if(col2)
{
model = IsNum(model) ? model.tointeger() : pGetVehicleModelFromName(model);
CreateVehicle(model.tointeger(), world.tointeger(), Vector(player.Pos.x,player.Pos.y,player.Pos.z), player.Angle, col1.tointeger(), col2.tointeger());
QuerySQL(Vehicles, "INSERT INTO Vehicles(ID, Model, World, Pos_X, Pos_Y, Pos_Z, Angle, Colour_1, Colour_2) VALUES ('"+GetVehicleCount()+"', '"+model+"', '"+world+"', '"+player.Pos.x+"', '"+player.Pos.y+"', '"+player.Pos.z+"', '"+player.Angle+"', '"+col1+"', '"+col2+"')");
MessagePlayer("[#00FF00][SUCCESS]:[#FFFFFF] Added a vehicle into database ID: "+GetVehicleCount()+", Name: "+GetVehicleNameFromModel(model)+", Model ID: "+model+"",player);
}
else ERROR("You must specify the Colour 1 for creating vehicle!",player);
}
else ERROR("You must specify the Colour 2 for creating vehicle!",player);
}
else ERROR("You must specify the world ID for creating vehicle!",player);
}
else ERROR("You must specify the model ID for creating vehicle!",player);
}
else ERROR("You must be spawned to add a car to database!",player);
}
else MessagePlayer("[#FFFF00]Syntax; /"+cmd+" <Model ID> <World ID> <Colour 1> <Colour 2>",player);
}
else ERROR("Unknown Command!",player);
}

else if(cmd == "removecar")
{
if(player.Name == "MR_SK") //you can edit this line as your wish
{
if(player.IsSpawned)
{
local veh = player.Vehicle;
if(veh)
{
if(QuerySQL(Vehicles, "SELECT * FROM Vehicles WHERE ID='"+veh.ID+"'"))
{
QuerySQL(Vehicles, "DELETE FROM Vehicles WHERE ID='"+veh.ID+"'");
MessagePlayer("[#00FF00][SUCCESS]:[#FFFFFF] Removed Vehicle ID: "+veh.ID+"",player);
veh.Delete();
UpdateVehiclesID();
ReloadVehicles();
}
else ERROR("Server was unable to find this vehicle in database!",player);
}
else ERROR("You must be in any vehicle to edit it's data!",player);
}
else ERROR("You must be spawned to add a car to database!",player);
}
else ERROR("Unknown Command!",player);
}

else if(cmd == "editveh" ||  cmd == "updateveh" || cmd == "updatecar" || cmd == "editcar")
{
if(player.Name == "MR_SK") //you can edit this line as your wish
{
if(player.IsSpawned)
{
local veh = player.Vehicle;
if(veh)
{
local model = GetTok(text, " ", 1), world = GetTok(text, " ", 2), col1 = GetTok(text, " ", 3), col2 = GetTok(text, " ", 4);
if(model)
{
if(world)
{
if(col1)
{
if(col2)
{
model = IsNum(model) ? model.tointeger() : pGetVehicleModelFromName(model);
if(QuerySQL(Vehicles, "SELECT * FROM Vehicles WHERE ID='"+veh.ID+"'"))
{
QuerySQL(Vehicles, "UPDATE Vehicles SET Model='"+model+"', World='"+world+"', Pos_X='"+veh.Pos.x+"', Pos_Y='"+veh.Pos.y+"', Pos_Z='"+veh.Pos.z+"', Angle='"+veh.Angle+"', Colour_1='"+col1+"', Colour_2='"+col2+"' WHERE ID='"+veh.ID+"'");
MessagePlayer("[#00FF00][SUCCESS]:[#FFFFFF] Vehicle Updated ID: "+veh.ID+", Name: "+GetVehicleNameFromModel(model)+", Model ID: "+model+"",player);
ReloadVehicles();
}
else ERROR("Server was unable to find this vehicle in database!",player);
}
else ERROR("You must specify the Colour 1 for creating vehicle!",player);
}
else ERROR("You must specify the Colour 2 for creating vehicle!",player);
}
else ERROR("You must specify the world ID for creating vehicle!",player);
}
else ERROR("You must specify the model ID for creating vehicle!",player);
}
else ERROR("You must be in any vehicle to edit it's data!",player);
}
else ERROR("You must be spawned to update or edit any car data!",player);
}
else ERROR("Unknown Command!",player);
}
[/noae]

And in last some functions in the vehicle system paste these functions in your script anywhere

[noae]function UpdateVehiclesID()
{
local newID = 0, q;
for(local i = 0; i < 500; ++i)
{
if(q = QuerySQL(Vehicles, "SELECT * FROM Vehicles WHERE ID='"+i+"'"))
{
newID++;
QuerySQL(Vehicles, "UPDATE Vehicles SET ID='"+newID+"' WHERE ID LIKE '"+i+"'");
}
}
}

function LoadVehicles()
{
local totalveh = 0;
for(local i = 0; i < 500; ++i)
{
local q = QuerySQL(Vehicles, "SELECT * FROM Vehicles WHERE ID='"+i+"'");
if(q)
{
CreateVehicle(GetSQLColumnData(q, 1), GetSQLColumnData(q, 2), Vector(GetSQLColumnData(q,3),GetSQLColumnData(q,4),GetSQLColumnData(q,5)), GetSQLColumnData(q,6), GetSQLColumnData(q,7), GetSQLColumnData(q,8));
totalveh+=1;
}
}
print(""+totalveh+" Vehicles has been loaded successfuly!");
}

function ReloadVehicles()
{
for(local i = 0; i < 500; ++i)
{
local veh = FindVehicle(i);
if(veh)
{
veh.Delete();
}
}
LoadVehicles();
}

function ERROR(string, player)
{
MessagePlayer("[#FF0000][ERROR]: "+string,player);
}

function pGetVehicleModelFromName(model)
{
model = model.tolower();
if(model.find("hydra") != null) return 6420; //you can continue with adding more line to this functions if you've more custom vehicles just change the vehicle name and model ID

else{
return GetVehicleModelFromName(model);
}
}

function GetTok(string, separator, n, ...)
{
 local m = vargv.len() > 0 ? vargv[0] : n,
    tokenized = split(string, separator),
    text = "";
 
 if (n > tokenized.len() || n < 1) return null;
 for (; n <= m; n++)
 {
  text += text == "" ? tokenized[n-1] : separator + tokenized[n-1];
 }
 return text;
}
[/noae]

The code is tested yet and no bug was found if you found any bug in this code then don't hesitate to reply to this post as it can save newbies time :P
@mR_Sk@