Problem with vehicle system

Started by John, Jan 03, 2019, 03:51 PM

Previous topic - Next topic

John

Hi, the vehicle system that im using is showing null when I Enter vehicle instead of showing Owner's name and cost etc. Vehicle Entered: Infernus. Cost: (null : 0x(nil)). Owner: (null : 0x(nil)). Shares: (null : 0x(nil)) & (null : 0x(nil)).

Here is the code,

Functions:
[noae]function LoadVehicles()
{
local q = QuerySQL( Vehicles, "SELECT * FROM Creation" ), i = 0;
while( GetSQLColumnData( q, 0 ) )
{
local
Model= GetSQLColumnData( q, 1 ),
X= GetSQLColumnData( q, 2 ),
Y= GetSQLColumnData( q, 3 ),
Z= GetSQLColumnData( q, 4 ),
Col1= GetSQLColumnData( q, 5 ),
Col2= GetSQLColumnData( q, 6 ),
World= GetSQLColumnData( q, 7 ),
Angle= GetSQLColumnData( q, 8 );
CreateVehicle( Model, World, Vector(X, Y, Z), Angle, Col1, Col2 );
GetSQLNextRow( q );
i++;
}
print("Vehicles Count ("+i+")");
return 0;
}

function IsVehForSale( id )
{
local q = QuerySQL( Vehicles, "SELECT * FROM Sale WHERE ID='"+id+"'" );
local own = GetSQLColumnData( q, 2 );
if ( own == "Unowned" ) return 1;
else return 0;
}
function IsOwnerOrSharer( veh, player )
{
local q = QuerySQL( Vehicles, "SELECT * FROM Sale WHERE ID='"+veh.ID+"'" );
local owner = GetSQLColumnData( q, 2 );
local sha = GetSQLColumnData( q, 3 );
local sha2 = GetSQLColumnData( q, 4 );
if ( ( owner == player.Name ) || ( sha == player.Name ) || ( sha2 == player.Name ) ) return 1;
else return 0;
}
function IsVehicleOwner( veh, player )
{
local q = QuerySQL( Vehicles, "SELECT * FROM Sale WHERE ID='"+veh.ID+"'" );
local owner = GetSQLColumnData( q, 2 );
if ( owner == player.Name ) return 1;
else return 0;
}

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;
}

function NumTok(string, separator)
{
local tokenized = split(string, separator);
return tokenized.len();
}

function GetPlayer( plr )
{
if ( plr )
{
if ( IsNum( plr ) )
{
plr = FindPlayer( plr.tointeger() );
if ( plr ) return plr;
else return false;
}
else
{
plr = FindPlayer( plr );
if ( plr ) return plr;
else return false;
}
}
else return false;
}
[/noae]

OnScriptLoad:
[noae]Vehicles <- ConnectSQL( "Vehicles.db" );

QuerySQL(Vehicles, "CREATE TABLE IF NOT EXISTS Creation ( id NUMERIC, model NUMERIC, x NUMERIC, y NUMERIC, z NUMERIC, col1 NUMERIC, col2 NUMERIC, world NUMERIC, angle NUMERIC)" );
QuerySQL(Vehicles, "CREATE TABLE IF NOT EXISTS Sale ( ID NUMERIC, Cost NUMERIC, Owner TEXT, Shared TEXT, Shared2 TEXT )" );

LoadVehicles();
[/noae]

Commands:
[noae]else if (cmd=="changecol")
{
if( !text ) MessagePlayer("Use /"+cmd+" <Veh/ID> <Col1/ID> <Col2/ID>.", player);
else if ( !player.IsSpawned ) MessagePlayer("You need be spawned to use this command.", player );
else {
local TextSplit = split( text, " " ),VehicleID=TextSplit[ 0 ],ColID1=TextSplit[ 1 ],ColID2=TextSplit[ 2 ];
if ( !IsNum(VehicleID)) MessagePlayer("Use /"+cmd+" <Veh/ID> <Col1/ID> <Col2/ID>.", player);
else if ( !IsNum(ColID1)) MessagePlayer("Use /"+cmd+" <Veh/ID> <Col1/ID> <Col2/ID>.", player);
else if ( !IsNum(ColID2)) MessagePlayer("Use /"+cmd+" <Veh/ID> <Col1/ID> <Col2/ID>.", player);
else if ( !FindVehicle(VehicleID.tointeger())) MessagePlayer("Vehicle ID does not exist.",player);
else {
local v=FindVehicle(VehicleID.tointeger())
v.Colour1=ColID1.tointeger();
v.Colour2=ColID2.tointeger();
QuerySQL( Vehicles,"UPDATE Creation SET Col1='"+ColID1+"' WHERE ID='"+VehicleID+"'");
QuerySQL( Vehicles,"UPDATE Creation SET Col2='"+ColID2+"' WHERE ID='"+VehicleID+"'");
//Message("Admin ["+player.Name+"] changed the color of the vehicle ID: "+VehicleID+". Colour1 ID: "+ColID1+". Colour2 ID: "+ColID2+".");
     }
     }
}

else if (cmd=="addcar")
{
if ( stats[ player.ID ].level >= 5 )
{
if ( player.Vehicle ) MessagePlayer("You must be outside of the vehicle.", player );
else if ( !player.IsSpawned ) MessagePlayer("You need be spawned to use this command.", player );
else if( !text ) MessagePlayer("Use /"+cmd+" <Veh/ID> <Col1/ID> <Col2/ID> <World> <Price>.", player);
else {
local TextSplit = split( text, " " ),ModelID=TextSplit[ 0 ],ColID1=TextSplit[ 1 ],ColID2=TextSplit[ 2 ],WorldID=TextSplit[ 3 ],Price=TextSplit[ 4 ];
if (!IsNum(ModelID)) MessagePlayer("Use /"+cmd+" <Veh/ID> <Col1/ID> <Col2/ID> <World> <Price>.", player);
else if ( !IsNum(ColID1)) MessagePlayer("Use /"+cmd+" <Veh/ID> <Col1/ID> <Col2/ID> <World> <Price>.", player);
else if ( !IsNum(ColID2)) MessagePlayer("Use /"+cmd+" <Veh/ID> <Col1/ID> <Col2/ID> <World> <Price>.", player);
else if ( !IsNum(WorldID)) MessagePlayer("Use /"+cmd+" <Veh/ID> <Col1/ID> <Col2/ID> <World> <Price>.", player);
else if ( !IsNum(Price)) MessagePlayer("Use /"+cmd+" <Veh/ID> <Col1/ID> <Col2/ID> <World> <Price>.", player);
else if ( !Price ) MessagePlayer("Use /"+cmd+" <Veh/ID> <Col1/ID> <Col2/ID> <World> <Price>.", player);
else if ((ModelID.tointeger() < 130) ) MessagePlayer ( "The models ID must be greater than 130" , player );
else if ((ModelID.tointeger() == 180))MessagePlayer ("This model ID is not a vehicle" , player );
else if ((ModelID.tointeger() == 181))MessagePlayer ("This model ID is not a vehicle" , player );
else if ((ModelID.tointeger() == 165))MessagePlayer ("This model ID is not a vehicle" , player );
else {
local VehicleID=GetVehicleCount();
VehicleID+=1;
QuerySQL( Vehicles, "INSERT INTO Creation ( id, model, x, y, z, col1, col2, world, angle ) VALUES ( '"+VehicleID+"', '"+ModelID+"', '"+player.Pos.x+"', '"+player.Pos.y+"', '"+player.Pos.z+"', '"+ColID1+"', '"+ColID2+"', '"+WorldID+"', '"+player.Angle+"' )" );
QuerySQL( Vehicles, "INSERT INTO Sale ( ID, Cost, Owner, Shared, Shared2 ) VALUES ( '"+VehicleID+"', '"+Price+"', 'Unowned', 'None', 'None' )" );
CreateVehicle(ModelID.tointeger(),WorldID.tointeger(),player.Pos, player.Angle,ColID1.tointeger(),ColID2.tointeger());
//Message( "Admin ["+player.Name+"] Has added a new Vehicle ID: "+VehicleID+"." );
player.Pos.z +6;
     }
     }
}
}

else if ( cmd == "removecar" )
{
if ( stats[ player.ID ].level >= 5 )
{
if ( !player.IsSpawned ) MessagePlayer("You need be spawned to use this command.", player );
else if ( !text ) MessagePlayer("Use /"+cmd+" <Veh/ID>.", player);
else {   
local veh = FindVehicle( text.tointeger() );
if ( !veh ) MessagePlayer("Invalid Vehicle ID.", player );
else {
veh.Delete()
QuerySQL( Vehicles, "DELETE FROM Creation WHERE ID='"+veh.ID+"'" );
QuerySQL( Vehicles, "DELETE FROM Sale WHERE ID='"+veh.ID+"'" );
//Message( "Admin ["+player.Name+"] Removed car ID: "+veh.ID+".");
     }
     }
}
}

else if ( cmd == "setowner" )
{
if ( stats[ player.ID ].level >= 5 )
{
if ( !text ) MessagePlayer("Use /"+cmd+" <Nick/ID> <Veh/ID>.", player);
else {
local plr = GetPlayer( GetTok( text, " ", 1 ) );
if ( !plr ) MessagePlayer("Invalid player.", player );
else {
local veh = FindVehicle( GetTok( text, " ", 2 ).tointeger() );
if ( !veh ) MessagePlayer("Invalid Vehicle ID.", player );
else {
local q = QuerySQL( Vehicles, "SELECT * FROM Sale WHERE ID='"+veh.ID+"'" );
QuerySQL( Vehicles, "UPDATE Sale SET Owner='"+player.Name+"' WHERE ID='"+veh.ID+"'" );
//Message( "Admin ["+player.Name+"] Set Vehicle ID "+veh.ID+" owner to ["+plr.Name+"]." );
}
}
}
}
}

else if ( (cmd == "carforsale") || (cmd == "carsforsale") )
{
local V2="";
local V=false;
for( local VehicleID = 0; VehicleID <= 1000; VehicleID++ )
{
local q = QuerySQL( Vehicles, "SELECT * FROM Sale WHERE ID='"+VehicleID+"'" );
local owner = GetSQLColumnData( q, 2 );
if("Unowned" == owner)
{
V=true;
V2+=""+VehicleID+",";
}
}
if (V==true) MessagePlayer("Vehicles ID: "+V2+"",player)
if (V==false) MessagePlayer( "No cars for sale.", player );
}

else if ( cmd == "buycar" )
{
if ( !text ) MessagePlayer("Use /"+cmd+" <Veh/ID>.", player);
else if ( !IsNum( text ) ) MessagePlayer("ID Must be numbers.", player );
else if ( !IsVehForSale( text ) ) MessagePlayer("This vehicle is not for sale.", player );
else {
local q = QuerySQL( Vehicles, "SELECT * FROM Sale WHERE ID='"+text+"'" );
local Cost = GetSQLColumnData( q, 1 );
if ( player.Cash < Cost ) MessagePlayer("Not enought money.", player );
else
{
QuerySQL( Vehicles, "UPDATE Sale SET Owner='"+player.Name+"' WHERE ID='"+text+"'" );
MessagePlayer( "You have bought Vehicle ID "+text+".", player );
player.Cash -= Cost;
}
}
}

else if ( cmd == "mycars" )
{
local V2="";
local V=false;
for( local VehicleID = 0; VehicleID <= 1000; VehicleID++ )
{
local q = QuerySQL( Vehicles, "SELECT * FROM Sale WHERE ID='"+VehicleID+"'" );
local owner = GetSQLColumnData( q, 2 );
if(player.Name == owner)
{
V=true;
V2+=""+VehicleID+",";
}
}
if (V==true) MessagePlayer("Vehicles ID: "+V2+"",player)
if (V==false) MessagePlayer( "You don't own any vehicle.", player );
}

else if ( (cmd == "mysharedcars") || (cmd == "mysharecars") )
{
local V2="";
local V=false;
for( local VehicleID = 0; VehicleID <= 1000; VehicleID++ )
{
local q = QuerySQL( Vehicles, "SELECT * FROM Sale WHERE ID='"+VehicleID+"'" );
local shared = ( GetSQLColumnData( q, 3 ) || GetSQLColumnData( q, 4 ) );
if(player.Name == shared)
{
local Sha=GetSQLColumnData( q, 2 );
if( Sha != "None" )
{
V=true;
V2+="("+VehicleID+" by "+Sha+") ";
}
}
}
if (V==true) MessagePlayer("Vehicles Shareds ID: "+V2+".",player)
if (V==false) MessagePlayer( "You don't have any shared vehicle.", player );
}

else if ( cmd == "getcar" )
{
if ( !text ) MessagePlayer("Use /"+cmd+" <Veh/ID>.", player);
else if ( !IsNum( text ) ) MessagePlayer("Only put <IDs>.", player );
else {
local veh = FindVehicle( text.tointeger() );
if ( !veh ) MessagePlayer("Invalid Vehicle ID.", player );
else if ( !IsOwnerOrSharer( veh, player ) ) MessagePlayer("You don't own this vehicle.", player );
else {
MessagePlayer("You have spawned a "+GetVehicleNameFromModel(veh.Model)+".",player );
veh.Pos = Vector( player.Pos.x + 1, player.Pos.y+1, player.Pos.z  );
}
}
}

else if ( cmd == "driver" )
{
if ( !text ) MessagePlayer("Use /"+cmd+" <Veh/ID>.", player);
else if ( !IsNum( text ) ) MessagePlayer("Only put <IDs>.", player );
else {
local veh = FindVehicle( text.tointeger() );
if ( !veh ) MessagePlayer("Invalid Vehicle ID.", player );
else if ( !IsOwnerOrSharer( veh, player ) ) MessagePlayer("You don't own this vehicle.", player );
else if ( !veh.Driver) MessagePlayer("None is driving your vehicle.", player );
else {
MessagePlayer("["+veh.Driver+"] is driving Vehicle ID: "+veh.ID+".",player );
}
}
}

else if ( cmd == "sellcar" )
{
if ( !text ) MessagePlayer("Use /"+cmd+" <Veh/ID>.", player);
else if ( !IsNum( text ) ) MessagePlayer("ID Must be numbers.", player );
else {
local q = QuerySQL( Vehicles, "SELECT * FROM Sale WHERE ID='"+text+"'" );
local owner = GetSQLColumnData( q, 2 );
if ( owner != player.Name ) MessagePlayer("You don't own this vehicle.", player );
else
{
local Cost= GetSQLColumnData( q, 1 );
MessagePlayer( "You have sell your vehicle: " + GetVehicleNameFromModel( FindVehicle( text.tointeger() ).Model ) + " ID "+text+".", player );
QuerySQL( Vehicles, "UPDATE Sale SET Owner='Unowned' WHERE ID='"+text+"'" );
QuerySQL( Vehicles, "UPDATE Sale SET Shared='None' WHERE ID='"+text+"'" );
QuerySQL( Vehicles, "UPDATE Sale SET Shared2='None' WHERE ID='"+text+"'" );
/*
IncCash( player, Cost ); //Add your cash function.
*/
}
}
}

else if ( cmd == "sharecar" )
{
if ( !text ) MessagePlayer("Use /"+cmd+" <Veh/ID> <Nick/ID>.", player);
else {
local veh = FindVehicle( GetTok( text, " ", 1 ).tointeger() );
if ( !veh ) MessagePlayer( "Invalid Vehicle ID.", player );
else if ( !IsVehicleOwner( veh, player ) ) MessagePlayer("You are not the owner of this vehicle.", player);
else {
local plr = GetPlayer( GetTok( text, " ", 2 ) );
if ( !plr ) MessagePlayer("Invalid Player.", player);
else if ( plr.Name==player.Name ) MessagePlayer( "You share can't your car with yourself.", player );
else {
local q = QuerySQL( Vehicles, "SELECT * FROM Sale WHERE ID='"+veh.ID+"'" );
local shared1 = GetSQLColumnData( q, 3 )
if ( shared1 == "None" )
{
local q = QuerySQL(Vehicles,"SELECT * FROM Sale WHERE ID='"+veh.ID+"'");
if ( GetSQLColumnData(q, 4) == plr.Name ) MessagePlayer("You already have shared this car with him.", player );
else {
QuerySQL( Vehicles, "UPDATE Sale SET Shared='"+plr.Name+"' WHERE ID='"+veh.ID+"'" );
MessagePlayer( "You have shared your vehicle ID "+veh.ID+" with "+plr.Name+".", player );
MessagePlayer( ""+player.Name+" Shared Vehicle ID " + veh.ID + " with you.", plr );
}
}
else {
local shared2 = GetSQLColumnData( q, 4 )
if ( shared2 == "None" )
{
local q = QuerySQL(Vehicles,"SELECT * FROM Sale WHERE ID='"+veh.ID+"'");
if ( GetSQLColumnData(q, 3) == plr.Name ) MessagePlayer("You already have shared this car with him.", player );
else {
QuerySQL( Vehicles, "UPDATE Sale SET Shared2='"+plr.Name+"' WHERE ID='"+veh.ID+"'" );
MessagePlayer( "You have shared your vehicle ID "+veh.ID+" with "+plr.Name+".", player );
MessagePlayer( ""+player.Name+" Shared Vehicle ID " + veh.ID + " with you.", plr );
}
}
else MessagePlayer( "Full Slots.", player );
}
}
}
}
}

else if ( cmd == "delsharecar" )
{
if ( !text ) MessagePlayer("Use /"+cmd+" <Veh/ID> <Slot>.", player);
else {
local veh = GetTok( text, " ", 1 );
if ( !veh ) MessagePlayer( "Invalid Vehicle ID.", player );
else if (!IsNum (veh)) MessagePlayer( "Only Numbers.", player );
else {
local q = QuerySQL( Vehicles, "SELECT * FROM Sale WHERE ID='"+veh+"'" );
local owner = GetSQLColumnData( q, 2 );
if ( owner != player.Name ) MessagePlayer("You don't own this vehicle.", player );
else {
local id = GetTok( text, " ", 2 );
if ( !id ) MessagePlayer( "Put slot ID.", player );
else if (!IsNum (id)) MessagePlayer( "Only Numbers", player );
else {
    if ( id == "1" )
{
    QuerySQL( Vehicles, "UPDATE Sale SET Shared='None' WHERE ID='"+veh+"'" );
    MessagePlayer( "You've Deleted your share slot 1 from this Vehicle.", player );
}
if ( id == "2" )
{
    QuerySQL( Vehicles, "UPDATE Sale SET Shared2='None' WHERE ID='"+veh+"'" );
    MessagePlayer( "You've Deleted your share slot 2 from this Vehicle.", player );
}
if ( id >= "3" )
{
MessagePlayer( "Only slot 1-2.", player );
}
    }
    }
    }
    }
}

else if ( cmd == "carcmds" )
{
MessagePlayer("[#00a81e](/) buycar, sellcar, sharecar, delsharecar, driver, getcar, mysharedcars, mycars, carsforsale, changepos, changecol.", player);
}
[/noae]

OnPlayerEnterVehicle:
[noae]function onPlayerEnterVehicle( player, vehicle, door )
{
local VehicleID2 = QuerySQL( Vehicles, "SELECT * FROM Sale" );
if(VehicleID2)
{
local veh = player.Vehicle;
local q = QuerySQL( Vehicles, "SELECT * FROM Sale WHERE ID='"+veh.ID+"'" );
local Cost=GetSQLColumnData( q, 1 );
local Owner=GetSQLColumnData( q, 2 );
local Share=GetSQLColumnData( q, 3 );
local Sharer=GetSQLColumnData( q, 4 );
MessagePlayer( "[#0087ff]Vehicle Entered: "+GetVehicleNameFromModel( vehicle.Model )+". Cost: "+Cost+".", player );
MessagePlayer( "[#0087ff]Owner: "+Owner+". Shares: "+Share+" & "+Sharer+".", player );
}
}
[/noae]

Gito Baloch

hy sorry for the bump but im getting this bug 
AN ERROR HAS OCCURED [cannot convert the string]

CALLSTACK
*FUNCTION [GiveSpawnwep()] scripts/main.nut line [124]
*FUNCTION [onPlayerSpawn()] scripts/main.nut line [370]

LOCALS
[ID] ""
[Nweps] 3
[khe] " 26 20"
1
[player] INSTANCE
[this] TABLE
[player] INSTANCE
Programming is the language I speak, the world I shape, and the future I code

habi

today i was learning sql and this topic came to my notice. Running through the codes, i think the problem is in onPlayerEnterVehicle
Quote from: John on Jan 03, 2019, 03:51 PMlocal veh = player.Vehicle;
local q = QuerySQL( Vehicles, "SELECT * FROM Sale WHERE ID='"+veh.ID+"'" );

you have used veh.ID

hope this helps somebody