PDA

View Full Version : How can I add /givekey


Varg
29/07/2018, 10:22 AM
Hi,

How can i add givekey/takekey for vehicles? And i wanna if user havent key, cant engine. Sorry for my bad English. Im not a pawn coder, im trying to be. So i need examples.

CarData enum;
enum carData {
carID,
carExists,
carModel,
carOwner,
Float:carPos[4],
carColor1,
carColor2,
carPaintjob,
carLocked,
carMods[14],
carImpounded,
carImpoundPrice,
carFaction,
carSiren,
carWeapons[5],
carAmmo[5],
carVehicle
};

/engine cmd;
CMD:motor(playerid, params[])
{
new vehicleid = GetPlayerVehicleID(playerid);

if (!IsEngineVehicle(vehicleid))
return SendErrorMessage(playerid, "Bu komutu araç içinde kullanabilirsin.");

if (GetPlayerState(playerid) != PLAYER_STATE_DRIVER)
return SendErrorMessage(playerid, "Sürücü değilsiniz.");

if (CoreVehicles[vehicleid][vehFuel] < 1)
return SendErrorMessage(playerid, "Aracın benzin deposu boş.");

if (ReturnVehicleHealth(vehicleid) <= 300)
return SendErrorMessage(playerid, "Araç hasarlı görünüyor, çalıştırılamaz.");

switch (GetEngineStatus(vehicleid))
{
case false:
{
SetEngineStatus(vehicleid, true);
ShowPlayerFooter(playerid, "You have ~g~started~w~ the engine!");
SendNearbyMessage(playerid, 30.0, COLOR_PURPLE, "** %s kontağı çevirip aracın motorunu çalıştırır..", ReturnName(playerid, 0));
}
case true:
{
SetEngineStatus(vehicleid, false);
ShowPlayerFooter(playerid, "You have ~r~stopped~w~ the engine!");
SendNearbyMessage(playerid, 30.0, COLOR_PURPLE, "** %s kontağı ç.", ReturnName(playerid, 0));
}
}
return 1;
}

/lock cmd;

CMD:lock(playerid, params[])
{
static
id = -1;

if (!IsPlayerInAnyVehicle(playerid) && (id = (House_Inside(playerid) == -1) ? (House_Nearest(playerid)) : (House_Inside(playerid))) != -1 && House_IsOwner(playerid, id))
{
if (!HouseData[id][houseLocked])
{
HouseData[id][houseLocked] = true;
House_Save(id);

ShowPlayerFooter(playerid, "You have ~r~locked~w~ your house!");
PlayerPlaySound(playerid, 1145, 0.0, 0.0, 0.0);
}
else
{
HouseData[id][houseLocked] = false;
House_Save(id);

ShowPlayerFooter(playerid, "You have ~g~unlocked~w~ your house!");
PlayerPlaySound(playerid, 1145, 0.0, 0.0, 0.0);
}
}
else if (!IsPlayerInAnyVehicle(playerid) && (id = (Business_Inside(playerid) == -1) ? (Business_Nearest(playerid)) : (Business_Inside(playerid))) != -1)
{
if (Business_IsOwner(playerid, id))
{
if (!BusinessData[id][bizLocked])
{
BusinessData[id][bizLocked] = true;

Business_Refresh(id);
Business_Save(id);

ShowPlayerFooter(playerid, "You have ~r~locked~w~ the business!");
PlayerPlaySound(playerid, 1145, 0.0, 0.0, 0.0);
}
else
{
BusinessData[id][bizLocked] = false;

Business_Refresh(id);
Business_Save(id);

ShowPlayerFooter(playerid, "You have ~g~unlocked~w~ the business!");
PlayerPlaySound(playerid, 1145, 0.0, 0.0, 0.0);
}
}
}
else if (!IsPlayerInAnyVehicle(playerid) && (id = (Entrance_Inside(playerid) == -1) ? (Entrance_Nearest(playerid)) : (Entrance_Inside(playerid))) != -1)
{
if (strlen(EntranceData[id][entrancePass]))
{
Dialog_Show(playerid, EntrancePass, DIALOG_STYLE_INPUT, "Entrance Pass", "Please enter the password for this entrance:", "Submit", "Cancel");
}
}
else if ((id = Car_Nearest(playerid)) != -1)
{
static
engine,
lights,
alarm,
doors,
bonnet,
boot,
objective;

GetVehicleParamsEx(CarData[id][carVehicle], engine, lights, alarm, doors, bonnet, boot, objective);

if (Car_IsOwner(playerid, id) || (PlayerData[playerid][pFaction] != -1 && CarData[id][carFaction] == GetFactionType(playerid)))
{
if (!CarData[id][carLocked])
{
CarData[id][carLocked] = true;
Car_Save(id);

ShowPlayerFooter(playerid, "You have ~r~locked~w~ the vehicle!");
PlayerPlaySound(playerid, 1145, 0.0, 0.0, 0.0);

SetVehicleParamsEx(CarData[id][carVehicle], engine, lights, alarm, 1, bonnet, boot, objective);
}
else
{
CarData[id][carLocked] = false;
Car_Save(id);

ShowPlayerFooter(playerid, "You have ~g~unlocked~w~ the vehicle!");
PlayerPlaySound(playerid, 1145, 0.0, 0.0, 0.0);

SetVehicleParamsEx(CarData[id][carVehicle], engine, lights, alarm, 0, bonnet, boot, objective);
}
}
}
else SendErrorMessage(playerid, "You are not in range of anything you can lock.");
return 1;
}

Rufio
29/07/2018, 11:10 AM
We need the CarData enum.

Florin48
29/07/2018, 11:20 AM
not necessarily, he can create a KeyCar variable [playerid] = -1; when connecting and after trying to make the order.

CMD:givekey(playerid, params[])
{
new id;
sscanf(params,"u",id)) return SendClientMessage(playerid, -1, "/givekey [username/id]");
if(!IsPlayerInVehicle(playerid, /*Your vehicle*/)) return /*Message*/
KeyCar[id] = /*Your vehicle ID*/
...
return 1;
}

it's just a simple exemple, you must to try to make your own commands

Rufio
29/07/2018, 11:24 AM
not necessarily, he can create a KeyCar variable [playerid] = -1; when connecting and after trying to make the order.

CMD:givekey(playerid, params[])
{
new id;
sscanf(params,"u",id)) return SendClientMessage(playerid, -1, "/givekey [username/id]");
if(!IsPlayerInVehicle(playerid, /*Your vehicle*/)) return /*Message*/
KeyCar[id] = /*Your vehicle ID*/
...
return 1;
}

it's just a simple exemple, you must to try to make your own commands

While this will work, it's bad practise. There is a reason why vehicles have an enum, you're offering OP to write un-organized and potentially hard to read code. Not to even mention it won't be possible to save the given key, the key will be reset once the player disconnects which makes the function nearly useless.

Florin48
29/07/2018, 11:35 AM
While this will work, it's bad practise. There is a reason why vehicles have an enum, you're offering OP to write un-organized and potentially hard to read code. Not to even mention it won't be possible to save the given key, the key will be reset once the player disconnects which makes the function nearly useless.

why you want to give him the keys forever, you can sell his vehicle directly then, the order is just to give him keys from the vehicle for a short period of time, I do not think he needs rescue.

Rufio
29/07/2018, 11:39 AM
why you want to give him the keys forever, you can sell his vehicle directly then, the order is just to give him keys from the vehicle for a short period of time, I do not think he needs rescue.

Did I even give the keys "forever"? What you are doing is bad practise as the player might lose connection. If someone sells their vehicle, they won't have access to their vehicle while shared keys let both parties drive the said vehicle.

Varg
29/07/2018, 11:42 AM
We need the CarData enum.

CarData enum;
enum carData {
carID,
carExists,
carModel,
carOwner,
Float:carPos[4],
carColor1,
carColor2,
carPaintjob,
carLocked,
carMods[14],
carImpounded,
carImpoundPrice,
carFaction,
carSiren,
carWeapons[5],
carAmmo[5],
carVehicle
};

Florin48
29/07/2018, 11:45 AM
Did I even give the keys "forever"? What you are doing is bad practise as the player might lose connection. If someone sells their vehicle, they won't have access to their vehicle while shared keys let both parties drive the said vehicle.

yes, forever or as long as that player has the keys to the car and if one is upset about the other and steals the car because he has a key to it and starts to throw it in the water and she loses insurance points the one who owns the vehicle will be good of payment.

Rufio
29/07/2018, 12:02 PM
yes, forever or as long as that player has the keys to the car and if one is upset about the other and steals the car because he has a key to it and starts to throw it in the water and she loses insurance points the one who owns the vehicle will be good of payment.

What lol okay



CMD:givekey(playerid, params[]) { // if the command is entered by the player, notice that if you don't have any params in your command, you can delete params[] but we'll use params.
new id, time, carid; // defining our params
if(sscanf(params, "udd", id, carid, time)) return SendClientMessage(playerid, -1, "[USAGE]: /givekey <player name/ID> <car ID> <time(in minutes)>");
if(CarData[carid][carOwner] != PlayerData[playerid][ID]) return SendClientMessage(playerid, -1, "[ERROR]: You can't give a car that isn't yours!"); // edit the variables according to your needs, this basically checks if the car owner is equal to the player ID
if(!IsPlayerConnected(id)) return SendClientMessage(playerid, -1, "[ERROR]: Specified player isn't connected."); // checks if the specified ID's connected to the server
if(id == playerid) return SendClientMessage(playerid, -1, "[ERROR]: You can't give keys to yourself."); // checks if the specified ID is equal to the player ID
new Float:pX, Float:pY, Float:pZ; // defining position floats
GetPlayerPos(id, pX, pY, pZ); // getting the specified player's position
if(!IsPlayerInRangeOfPoint(playerid, 10.0, pX, pY, pZ)) return SendClientMessage(playerid, -1, "[ERROR]: You have to be close to the player to give keys."); // checking if the specified player's close to the player, 10.0 is the range feel free to delete this/edit the range to fit your needs
else { // if the player got no errors
CarData[carid][carShared] = PlayerData[id][ID]; // dont forget to add carShared to your CarData enum, this will hold the SQL ID of the specified player
CarData[carid][carShareTime] = time; // dont forget to add carShareTime to your CarData enum also dont forget this is in minutes.
new pName[MAX_PLAYER_NAME], tName[MAX_PLAYER_NAME], string[65];
GetPlayerName(playerid, pName, sizeof(pName)); // getting the player's name
GetPlayerName(id, tName, sizeof(tName)); // getting the specified player's name
format(string, sizeof(string), "You have given your car's keys to %s.", tName); // formatting the string
SendClientMessage(playerid, -1, string); // display a message
format(string, sizeof(string), "%s has given you keys to their car.", tName); // formatting the string
SendClientMessage(playerid, -1, string); // display a message
}
return 1;
}

This is a good base code for your givekey code, don't forget to check whether or not the player has keys to the car they've entered and let them drive the car if so, also don't forget to decrease the carShareTime every minute or so, I don't recommend using Timers for it as timers can put heavy load on the server and default timer library can be false in terms of time. Don't forget to change the variables' names if you have any errors, I have only guessed your variables' names. Also don't forget to remove the shared keys from the player once their time runs out.

Also, if you want multiple players to own the shared key instead of 1, you could try implementing the car sharing feature to playerdata instead of vehicledata and set their shared car key to the car ID. Good luck and let me know if you need further assistance.

Florin48
29/07/2018, 12:05 PM
What lol okay



CMD:givekey(playerid, params[]) { // if the command is entered by the player, notice that if you don't have any params in your command, you can delete params[] but we'll use params.
new id, time, carid; // defining our params
if(sscanf(params, "udd", id, carid, time)) return SendClientMessage(playerid, -1, "[USAGE]: /givekey <player name/ID> <car ID> <time(in minutes)>");
if(CarData[carid][carOwner] != PlayerData[playerid][ID]) return SendClientMessage(playerid, -1, "[ERROR]: You can't give a car that isn't yours!"); // edit the variables according to your needs, this basically checks if the car owner is equal to the player ID
if(!IsPlayerConnected(id)) return SendClientMessage(playerid, -1, "[ERROR]: Specified player isn't connected."); // checks if the specified ID's connected to the server
if(id == playerid) return SendClientMessage(playerid, -1, "[ERROR]: You can't give keys to yourself."); // checks if the specified ID is equal to the player ID
new Float:pX, Float:pY, Float:pZ; // defining position floats
GetPlayerPos(id, pX, pY, pZ); // getting the specified player's position
if(!IsPlayerInRangeOfPoint(playerid, 10.0, pX, pY, pZ)) return SendClientMessage(playerid, -1, "[ERROR]: You have to be close to the player to give keys."); // checking if the specified player's close to the player, 10.0 is the range feel free to delete this/edit the range to fit your needs
else { // if the player got no errors
CarData[carid][carShared] = PlayerData[id][ID]; // dont forget to add carShared to your CarData enum, this will hold the SQL ID of the specified player
CarData[carid][carShareTime] = time; // dont forget to add carShareTime to your CarData enum also dont forget this is in minutes.
new pName[MAX_PLAYER_NAME], tName[MAX_PLAYER_NAME], string[65];
GetPlayerName(playerid, pName, sizeof(pName)); // getting the player's name
GetPlayerName(id, tName, sizeof(tName)); // getting the specified player's name
format(string, sizeof(string), "You have given your car's keys to %s.", tName); // formatting the string
SendClientMessage(playerid, -1, string); // display a message
format(string, sizeof(string), "%s has given you keys to their car.", tName); // formatting the string
SendClientMessage(playerid, -1, string); // display a message
}
return 1;
}

This is a good base code for your givekey code, don't forget to check whether or not the player has keys to the car they've entered and let them drive the car if so, also don't forget to decrease the carShareTime every minute or so, I don't recommend using Timers for it as timers can put heavy load on the server and default timer library can be false in terms of time. Don't forget to change the variables' names if you have any errors, I have only guessed your variables' names. Also don't forget to remove the shared keys from the player once their time runs out.

Also, if you want multiple players to own the shared key instead of 1, you could try implementing the car sharing feature to playerdata instead of vehicledata and set their shared car key to the car ID. Good luck and let me know if you need further assistance.

good job, that will help him a lot.