Or use this
#include <a_samp>
#include <sqlitei> // Include SQLite plugin
// Constants
#define MAX_PROPERTIES 100
#define MAX_PROPERTY_NAME 32
// Variables
enum E_PROPERTY_DATA
{
PropertyID,
PropertyName[MAX_PROPERTY_NAME],
Float:PropertyX,
Float:PropertyY,
Float:PropertyZ,
PropertyPrice,
PropertyOwner[MAX_PLAYER_NAME]
};
new PropertyData[MAX_PROPERTIES][E_PROPERTY_DATA];
new TotalProperties = 0;
new DB:PropertyDB;
// Forward declarations
forward LoadProperties();
forward OnPropertyDataLoaded();
// Main function
main()
{
print("Property System Loaded.");
}
// OnGameModeInit
public OnGameModeInit()
{
// Open the SQLite database
PropertyDB = db_open("properties.db");
// Create the properties table if it doesn't exist
db_query(PropertyDB, "CREATE TABLE IF NOT EXISTS properties (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, x FLOAT, y FLOAT, z FLOAT, price INTEGER, owner TEXT)");
// Load properties from the database
LoadProperties();
return 1;
}
// OnGameModeExit
public OnGameModeExit()
{
// Close the database connection
db_close(PropertyDB);
return 1;
}
// Function to load properties from the database
public LoadProperties()
{
new DBResult:result = db_query(PropertyDB, "SELECT * FROM properties");
if (db_num_rows(result) > 0)
{
for (new i = 0; i < db_num_rows(result); i++)
{
PropertyData[PropertyID] = db_get_field_int(result, 0);
db_get_field(result, 1, PropertyData[PropertyName], MAX_PROPERTY_NAME);
PropertyData[PropertyX] = db_get_field_float(result, 2);
PropertyData[PropertyY] = db_get_field_float(result, 3);
PropertyData[PropertyZ] = db_get_field_float(result, 4);
PropertyData[PropertyPrice] = db_get_field_int(result, 5);
db_get_field(result, 6, PropertyData[PropertyOwner], MAX_PLAYER_NAME);
db_next_row(result);
TotalProperties++;
}
}
db_free_result(result);
// Callback when properties are loaded
OnPropertyDataLoaded();
return 1;
}
// Callback when property data is loaded
public OnPropertyDataLoaded()
{
printf("Loaded %d properties from the database.", TotalProperties);
return 1;
}
// Command to create a property
CMD:createproperty(playerid, params[])
{
new name[MAX_PROPERTY_NAME], price;
if (sscanf(params, "s[32]d", name, price))
return SendClientMessage(playerid, 0xFF0000AA, "Usage: /createproperty [name] [price]");
if (TotalProperties >= MAX_PROPERTIES)
return SendClientMessage(playerid, 0xFF0000AA, "Maximum properties reached!");
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z);
// Save property to the database
new query[256];
format(query, sizeof(query), "INSERT INTO properties (name, x, y, z, price, owner) VALUES ('%s', %f, %f, %f, %d, 'None')", name, x, y, z, price);
db_query(PropertyDB, query);
// Add property to the in-memory array
PropertyData[TotalProperties][PropertyID] = db_last_insert_rowid(PropertyDB);
strcpy(PropertyData[TotalProperties][PropertyName], name);
PropertyData[TotalProperties][PropertyX] = x;
PropertyData[TotalProperties][PropertyY] = y;
PropertyData[TotalProperties][PropertyZ] = z;
PropertyData[TotalProperties][PropertyPrice] = price;
strcpy(PropertyData[TotalProperties][PropertyOwner], "None");
TotalProperties++;
SendClientMessage(playerid, 0x00FF00AA, "Property created successfully!");
return 1;
}
// Command to buy a property
CMD:buyproperty(playerid, params[])
{
new propertyid;
if (sscanf(params, "d", propertyid))
return SendClientMessage(playerid, 0xFF0000AA, "Usage: /buyproperty [propertyid]");
if (propertyid < 0 || propertyid >= TotalProperties)
return SendClientMessage(playerid, 0xFF0000AA, "Invalid property ID!");
if (strcmp(PropertyData[propertyid][PropertyOwner], "None") != 0)
return SendClientMessage(playerid, 0xFF0000AA, "This property is already owned!");
// Check if the player has enough money
if (GetPlayerMoney(playerid) < PropertyData[propertyid][PropertyPrice])
return SendClientMessage(playerid, 0xFF0000AA, "You don't have enough money to buy this property!");
// Deduct money and assign ownership
GivePlayerMoney(playerid, -PropertyData[propertyid][PropertyPrice]);
strcpy(PropertyData[propertyid][PropertyOwner], GetPlayerName(playerid));
// Update the database
new query[128];
format(query, sizeof(query), "UPDATE properties SET owner = '%s' WHERE id = %d", GetPlayerName(playerid), PropertyData[propertyid][PropertyID]);
db_query(PropertyDB, query);
SendClientMessage(playerid, 0x00FF00AA, "You have successfully bought this property!");
return 1;
}
// Command to list properties
CMD:properties(playerid, params[])
{
for (new i = 0; i < TotalProperties; i++)
{
new msg[128];
format(msg, sizeof(msg), "Property ID: %d | Name: %s | Price: $%d | Owner: %s", i, PropertyData[PropertyName], PropertyData[PropertyPrice], PropertyData[PropertyOwner]);
SendClientMessage(playerid, 0xFFFFFFAA, msg);
}
return 1;
}
// Helper function to get player name
stock GetPlayerName(playerid)
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid, name, MAX_PLAYER_NAME);
return name;
}
It will save to db
#include <a_samp>
#include <sqlitei> // Include SQLite plugin
// Constants
#define MAX_PROPERTIES 100
#define MAX_PROPERTY_NAME 32
// Variables
enum E_PROPERTY_DATA
{
PropertyID,
PropertyName[MAX_PROPERTY_NAME],
Float:PropertyX,
Float:PropertyY,
Float:PropertyZ,
PropertyPrice,
PropertyOwner[MAX_PLAYER_NAME]
};
new PropertyData[MAX_PROPERTIES][E_PROPERTY_DATA];
new TotalProperties = 0;
new DB:PropertyDB;
// Forward declarations
forward LoadProperties();
forward OnPropertyDataLoaded();
// Main function
main()
{
print("Property System Loaded.");
}
// OnGameModeInit
public OnGameModeInit()
{
// Open the SQLite database
PropertyDB = db_open("properties.db");
// Create the properties table if it doesn't exist
db_query(PropertyDB, "CREATE TABLE IF NOT EXISTS properties (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, x FLOAT, y FLOAT, z FLOAT, price INTEGER, owner TEXT)");
// Load properties from the database
LoadProperties();
return 1;
}
// OnGameModeExit
public OnGameModeExit()
{
// Close the database connection
db_close(PropertyDB);
return 1;
}
// Function to load properties from the database
public LoadProperties()
{
new DBResult:result = db_query(PropertyDB, "SELECT * FROM properties");
if (db_num_rows(result) > 0)
{
for (new i = 0; i < db_num_rows(result); i++)
{
PropertyData[PropertyID] = db_get_field_int(result, 0);
db_get_field(result, 1, PropertyData[PropertyName], MAX_PROPERTY_NAME);
PropertyData[PropertyX] = db_get_field_float(result, 2);
PropertyData[PropertyY] = db_get_field_float(result, 3);
PropertyData[PropertyZ] = db_get_field_float(result, 4);
PropertyData[PropertyPrice] = db_get_field_int(result, 5);
db_get_field(result, 6, PropertyData[PropertyOwner], MAX_PLAYER_NAME);
db_next_row(result);
TotalProperties++;
}
}
db_free_result(result);
// Callback when properties are loaded
OnPropertyDataLoaded();
return 1;
}
// Callback when property data is loaded
public OnPropertyDataLoaded()
{
printf("Loaded %d properties from the database.", TotalProperties);
return 1;
}
// Command to create a property
CMD:createproperty(playerid, params[])
{
new name[MAX_PROPERTY_NAME], price;
if (sscanf(params, "s[32]d", name, price))
return SendClientMessage(playerid, 0xFF0000AA, "Usage: /createproperty [name] [price]");
if (TotalProperties >= MAX_PROPERTIES)
return SendClientMessage(playerid, 0xFF0000AA, "Maximum properties reached!");
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z);
// Save property to the database
new query[256];
format(query, sizeof(query), "INSERT INTO properties (name, x, y, z, price, owner) VALUES ('%s', %f, %f, %f, %d, 'None')", name, x, y, z, price);
db_query(PropertyDB, query);
// Add property to the in-memory array
PropertyData[TotalProperties][PropertyID] = db_last_insert_rowid(PropertyDB);
strcpy(PropertyData[TotalProperties][PropertyName], name);
PropertyData[TotalProperties][PropertyX] = x;
PropertyData[TotalProperties][PropertyY] = y;
PropertyData[TotalProperties][PropertyZ] = z;
PropertyData[TotalProperties][PropertyPrice] = price;
strcpy(PropertyData[TotalProperties][PropertyOwner], "None");
TotalProperties++;
SendClientMessage(playerid, 0x00FF00AA, "Property created successfully!");
return 1;
}
// Command to buy a property
CMD:buyproperty(playerid, params[])
{
new propertyid;
if (sscanf(params, "d", propertyid))
return SendClientMessage(playerid, 0xFF0000AA, "Usage: /buyproperty [propertyid]");
if (propertyid < 0 || propertyid >= TotalProperties)
return SendClientMessage(playerid, 0xFF0000AA, "Invalid property ID!");
if (strcmp(PropertyData[propertyid][PropertyOwner], "None") != 0)
return SendClientMessage(playerid, 0xFF0000AA, "This property is already owned!");
// Check if the player has enough money
if (GetPlayerMoney(playerid) < PropertyData[propertyid][PropertyPrice])
return SendClientMessage(playerid, 0xFF0000AA, "You don't have enough money to buy this property!");
// Deduct money and assign ownership
GivePlayerMoney(playerid, -PropertyData[propertyid][PropertyPrice]);
strcpy(PropertyData[propertyid][PropertyOwner], GetPlayerName(playerid));
// Update the database
new query[128];
format(query, sizeof(query), "UPDATE properties SET owner = '%s' WHERE id = %d", GetPlayerName(playerid), PropertyData[propertyid][PropertyID]);
db_query(PropertyDB, query);
SendClientMessage(playerid, 0x00FF00AA, "You have successfully bought this property!");
return 1;
}
// Command to list properties
CMD:properties(playerid, params[])
{
for (new i = 0; i < TotalProperties; i++)
{
new msg[128];
format(msg, sizeof(msg), "Property ID: %d | Name: %s | Price: $%d | Owner: %s", i, PropertyData[PropertyName], PropertyData[PropertyPrice], PropertyData[PropertyOwner]);
SendClientMessage(playerid, 0xFFFFFFAA, msg);
}
return 1;
}
// Helper function to get player name
stock GetPlayerName(playerid)
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid, name, MAX_PLAYER_NAME);
return name;
}
It will save to db