One thing I noticed is your use of string.find() for comparison. Is there a reason why you wouldn't just use == for comparison?
If someone was called Poop, and the vehicle is owned by PoopHead it will return true wouldn't it?
Another quick question about your sql_create script. You use "100,000" as price, which isn't an integer.
OOps, I haven't executed the SQL file yet, thanks working
EDIT:-
The DB file is empty, and i don't know how to add, so please do tell me
I have tried rulk , still then not working
I have vehicles using function.nut, how can i use it, I don't have any vehicles using server.conf file.
// Create your vehicles
dofile("CreateVehicles.nut");
// Load the sql_create file
dofile("sql_create.nut" );
// Call the main function from that file, to create blah.sql
sql_create();
2nd thing,,,
// First Create your vehicles
dofile( "CreateVehicles.nut" );
Now there's a function name LoadVehs();
Which have the CreateVehicle thing, now tell me what shall i do? or the script will do itself
Oh, Actually I was creating the cars after sql_create, so it wasn't working, THANKS, it working NOW!
else if ( cmd == "buycar" )
{
// Lets first establish if your in a vehicle
if ( player.Vehicle == null ) MessagePlayer( ">> Error: you are not in a vehicle.", player );
else
{
// OK, we're in a vehicle, lets get it's instance into a variable.
local vehicle = player.Vehicle;
// Put the IsPurchased property into a variable, so we can access squirrel's VM built in manipulation functions for that data type.
local val = MyVehicles[ vehicle.ID ].IsPurchased;
// First, check its an empty value ( it's available to buy )
if ( ( val == null ) || ( val.len() == 0 ) )
{
if( stats[ player.ID ].Cash >= MyVehicles[ vehicle.ID ].VehiclePrice )
{
// Then Set the value in our array.
MyVehicles[ vehicle.ID ].IsPurchased = player.Name;
// Display whats going on.
MessagePlayer( ">> You have now purchased this vehicle.", player );
//Reduce his money
stats[ player.ID ].Cash -= MyVehicles[ vehicle.ID ].VehiclePrice;
}
else MessagePlayer("You don't have enough money to buy this car.",player);
}
// It's got a value, and is purchased, therefore, not avilable to buy
else MessagePlayer( ">> Error: Vehicle not avilable to purchase.", player );
}
}
BUMP*
I want that each user can buy only 2 vehicles, how can i do it?
Any examples pl0x
Just check player vehiles through mycars function that if player already bought the 2 vehicles then throw a messageQuote from KAKAN on September 21st, 2015, 07:14 PM BUMP*
I want that each user can buy only 2 vehicles, how can i do it?
Any examples pl0x
function HowManyCars(player)
{
// Empty string for our output.
local output = "";
// Var to count how many vehicles the player ownes.
local count = 0;
// Iterate the array, using a foreach loop.
foreach ( obj in this[ MyArray ] )
{
if ( obj != null )
{
// Add the object properties to a tmp variable, so we can access squirrel's VM built in manipulation functions for that data type.
local tmp = obj.IsPurchased;
// Check the value is not null or empty
if ( ( tmp != null ) && ( tmp.len() > 0 ) )
{
// Perform a string comparison, if we get a match, increase our counter.
if ( tmp.find( player ) != null ) count++;
}
}
}
// return count as integer.
return count;
}
@KAKaN First Make A table VC in Which playername and count
on register iñsert into vc values nàme 0
then make GetVC and SetVC functions and add this on Buycar
else if ( GetVC( player ) >= 2 ) PrivMessage( player, "You already have two cars " );
else
{
SetVC( player, GetVC( player ) + 1 );
}
and same in. sell car -1
@kusanagi is right, just alter the MyCars algorithm, like this....Quote from Kusanagi on September 21st, 2015, 07:26 PM Just check player vehiles through mycars function that if player already bought the 2 vehicles then throw a messageQuote from KAKAN on September 21st, 2015, 07:14 PM BUMP*
I want that each user can buy only 2 vehicles, how can i do it?
Any examples pl0xCode: [Select] function HowManyCars(player)
{
// Empty string for our output.
local output = "";
// Var to count how many vehicles the player ownes.
local count = 0;
// Iterate the array, using a foreach loop.
foreach ( obj in this[ MyArray ] )
{
if ( obj != null )
{
// Add the object properties to a tmp variable, so we can access squirrel's VM built in manipulation functions for that data type.
local tmp = obj.IsPurchased;
// Check the value is not null or empty
if ( ( tmp != null ) && ( tmp.len() > 0 ) )
{
// Perform a string comparison, if we get a match, increase our counter.
if ( tmp.find( player ) != null ) count++;
}
}
}
// return count as integer.
return count;
}
if( HowManyCars(player) >= 2 ) MessagePlayer("You have enough vehicles",player);
MyVehicle[ vehicle.ID ].IsPurchased = null;
else if ( cmd == "sellcar" )
{
// A paremeter checks before we can transfer the vehicle.
if ( player.Vehicle == null ) MessagePlayer( ">> Error: you are not in a vehicle.", player );
else
{
// OK, we're in a vehicle, lets get it's instance into a variable.
local vehicle = player.Vehicle;
// Put the IsPurchased property into a variable, so we can access squirrel's VM built in manipulation functions for that data type.
local val = MyVehicles[ vehicle.ID ].IsPurchased;
// First, Check they own that vehicle.
if ( val == player.Name )
{
// OK, now Set the value in our array.
MyVehicles[ vehicle.ID ].IsPurchased == null;
//Return him the money
player.Cash += MyVehicles[ vehicle.ID ].VehiclePrice/2
// Display whats going on.
MessagePlayer( ">> You have now sold this vehicle", player );
}
// They do not own that vehicle.
else MessagePlayer( ">> Error: you do not own this vehicle.", player );
}
}
MyVehicles[ vehicle.ID ].IsPurchased = "";
MyVehicles[ vehicle.ID ].IsPurchased = "";
:( again empty, i did exactly as you said the message box appeared import succes but still its emty no vehicles there.
all vehicles are written in server.config and load when the server starts. i have the sql_create.nut in the folder were my server.exe is and the server.config. my other scripts are in the folder scripts.
Yes Thijn i have read :) the sql file when its created contains the following:
CREATE TABLE `MyVehicles` ( `VehicleID` INTEGER, `VehicleModel` TEXT, `VehicleCat` TEXT, `VehiclePrice` INTEGER, `IsPurchased` TEXT, `IsShared` TEXT );
and yes i load the sql_create.nut, ofcourse otherwize i wouldnt have the sql.blah... and yes when i start the server vehycles are there also in console msg vehicles loaded.
you can, but is that what your blah.sql looks like ?
local output = @"Model: " + MyVehicles[ vehicle.ID ].VehicleModel + " - " +
if ( cmd == "buycar" )
{
// Lets first establish if your in a vehicle
if ( player.Vehicle == null ) MessagePlayer( "[#ff0000]Error: you are not in a vehicle.", player );
else
{
// OK, we're in a vehicle, lets get it's instance into a variable.
local vehicle = player.Vehicle;
// Put the IsPurchased property into a variable, so we can access squirrel's VM built in manipulation functions for that data type.
local val = MyVehicles[ vehicle.ID ].IsPurchased;
// First, check its an empty value ( it's available to buy )
if ( ( val == null ) || ( val.len() == 0 ) )
{
// Then Set the value in our array.
MyVehicles[ vehicle.ID ].IsPurchased = player.Name;
// The array has been updated, so set our value to 'true' so we can save it to the database.
MyVehicles[ vehicle.ID ].Update = true;
// Display whats going on.
MessagePlayer( "[#ff0000]You have now purchased this vehicle.", player );
}
// It's got a value, and is purchased, therefore, not avilable to buy
else MessagePlayer( "[#ff0000]Error: Vehicle not avilable to purchase.", player );
}
}
if ( cmd == "buycar" )
{
// Lets first establish if your in a vehicle
if ( player.Vehicle == null ) MessagePlayer( "[#ff0000]Error: you are not in a vehicle.", player );
else
{
// OK, we're in a vehicle, lets get it's instance into a variable.
local vehicle = player.Vehicle;
// Put the IsPurchased property into a variable, so we can access squirrel's VM built in manipulation functions for that data type.
local val = MyVehicles[ vehicle.ID ].IsPurchased;
// First, check its an empty value ( it's available to buy )
if ( player.Cash < MyVehicles[vehicle.ID].VehiclePrice ) {
PM("[#ff0000]Not enought cash",player)
return false;
}
if ( ( val == null ) || ( val.len() == 0 ) )
{
// Then Set the value in our array.
MyVehicles[ vehicle.ID ].IsPurchased = player.Name;
// The array has been updated, so set our value to 'true' so we can save it to the database.
MyVehicles[ vehicle.ID ].Update = true;
player.Cash -= MyVehicles[vehicle.ID].VehiclePrice;
// Display whats going on.
MessagePlayer( "[#ff0000]You have now purchased this vehicle.", player );
}
// It's got a value, and is purchased, therefore, not avilable to buy
else MessagePlayer( "[#ff0000]Error: Vehicle not avilable to purchase.", player );
}
}
fixedCode: [Select] if ( cmd == "buycar" )
{
// Lets first establish if your in a vehicle
if ( player.Vehicle == null ) MessagePlayer( "[#ff0000]Error: you are not in a vehicle.", player );
else
{
// OK, we're in a vehicle, lets get it's instance into a variable.
local vehicle = player.Vehicle;
// Put the IsPurchased property into a variable, so we can access squirrel's VM built in manipulation functions for that data type.
local val = MyVehicles[ vehicle.ID ].IsPurchased;
// First, check its an empty value ( it's available to buy )
if ( player.Cash < MyVehicles[vehicle.ID].VehiclePrice ) {
PM("[#ff0000]Not enought cash",player)
return false;
}
if ( ( val == null ) || ( val.len() == 0 ) )
{
// Then Set the value in our array.
MyVehicles[ vehicle.ID ].IsPurchased = player.Name;
// The array has been updated, so set our value to 'true' so we can save it to the database.
MyVehicles[ vehicle.ID ].Update = true;
player.Cash -= MyVehicles[vehicle.ID].VehiclePrice;
// Display whats going on.
MessagePlayer( "[#ff0000]You have now purchased this vehicle.", player );
}
// It's got a value, and is purchased, therefore, not avilable to buy
else MessagePlayer( "[#ff0000]Error: Vehicle not avilable to purchase.", player );
}
}
also, when i saved custom vehicles, in sql browser, their names are null(0x0000000000) ;c
if( cmd == "newveh" ){
//Your work...
MyVehicles.push( SunshineAutos(
VehicleID = vehicle.ID,
VehicleModel = vehicle.Model,
VehicleCat = GetVehicleType( vehicle.Model ),
VehiclePrice = 1,
IsPurchased = false,
IsShared = null,
Update = true
);
//Update the database using SunshineAutos.SaveDatabase("sunshine/sunshine.db"); to save the changes.
}
if ( cmd == "add" )
{
if (!ReadIniBool("stats.ini","admin",player.UID2))
{
PM("nope",player)
return false;
}
if(!player.Vehicle)
{
PM("[#ff0000]You must be in car without stats in /car to add it",player)
return false;
}
SunshineAutos.LoadDatabase("sunshine/sunshine.db"); // getting error here!!!!!!!!!!
MyVehicles.push( SunshineAutos(
VehicleID = player.Vehicle.ID,
VehicleModel = player.Vehicle.Model,
VehicleCat = GetVehicleType( player.Vehicle.Model ),
VehiclePrice = rand()%100000,
IsPurchased = false,
IsShared = null,
Update = true
));
SunshineAutos.SaveDatabase("sunshine/sunshine.db");
PM("[#00ff00]Vehicle ID: "+player.Vehicle.ID+" pushed to database",player)
}
...
after this im getting
AN ERROR HAS OCCURED [trying to modify a class that has already been instantiated]
on line sunshineautos.loaddatabase
i commented it on code
and car doesnt get added to databse
note im noob in sqlite things
class Test
{
var0 = 24
}
// Ok. No instance of Test exists so far
test.rawnewmember("var1", 82);
// Make an instance of Test
local t = Test()
// Error. I've already created instances of Test. I can't modify it now
test.rawnewmember("var2", 127);
SunshineAutos.LoadDatabase("sunshine/sunshine.db"); // getting error here!!!!!!!!!!
yea but when i add vehicle ussing this
https://forum.vc-mp.org/?topic=1177.0
It gets in the sunshine db some how oor bugged and the database messes up, car names are messed, pcj is sanchez etc and random prices
i dont know how to fix it
function onPlayerEnterVehicle( player, veh, isPassenger )
{
local vehicle = player.Vehicle;
local output = @"Model: " + MyVehicles[ vehicle.ID ].VehicleModel + " - " +
"ID: " + MyVehicles[ vehicle.ID ].VehicleID + " - " +
"Type: " + MyVehicles[ vehicle.ID ].VehicleCat + " - " +
"Price: " + MyVehicles[ vehicle.ID ].VehiclePrice;
local
IsPurchased = MyVehicles[ vehicle.ID ].IsPurchased.len() > 0 ? MyVehicles[ vehicle.ID ].IsPurchased : "For Sale",
IsShared = MyVehicles[ vehicle.ID ].IsShared.len() > 0 ? MyVehicles[ vehicle.ID ].IsShared : "Not Shared";
output = output + " - " + "Owner: " + IsPurchased + " - " + "Sharer: " + IsShared;
MessagePlayer( output, player );
}