Vehicle Owner from Vehicle ID - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Vehicle Owner from Vehicle ID (
/showthread.php?tid=530032)
Vehicle Owner from Vehicle ID -
Alex_T - 05.08.2014
Okay I would like to find out the owner of a vehicle from just the ID...How would it be possible? What function would I use? Thanks for the help!
Re: Vehicle Owner from Vehicle ID -
Gintaras123 - 05.08.2014
Код:
/// an example
new mowner[MAX_VEHICLES][MAX_PLAYER_NAME]; // on top of your gm
////////////////
// onplayerstatechange
// if new state == driver
new mid = GetPlayerIdFromName(mowner[vehicleid]);
if(mid == playerid)
{
SendClientMessage(playerid,-1,"Welcome to your vehicle");
return 1;
}
new msgas[126];
format(msgas,sizeof(msgas),"Vehicle owned by %s",mowner[vehicleid]);
SendClientMessage(playerid,-1,msgas);
return 1;
}
GetPlayerName(playerid,mowner[vehicleid],24); /// to make player own a vehicle
stock GetPlayerIdFromName(playername[])
{
for(new i = 0; i <= MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
new playername2[MAX_PLAYER_NAME];
GetPlayerName(i, playername2, sizeof(playername2));
if(strcmp(playername2, playername, true, strlen(playername)) == 0)
{
return i;
}
}
}
return INVALID_PLAYER_ID;
}
////////////// owner of vehicle id from id
new mid = GetPlayerIdFromName(mowner[yourid]);
I'm not sure if code will work , because i didnt tested it yet.
Re: Vehicle Owner from Vehicle ID -
Dignity - 05.08.2014
Here's another method, which is (imo) more efficient than the above one. I came up with this a minute ago so it's not 100% as this will only return a name if the owner is connected. If not, it'll return a negative value to kill the command.
It's obviously not plug and play but should give you an idea of how to proceed. This method requires you to store the vehicle ID in to a player variable (and save/load it ofcourse) and then check if the value of that variable is the same as the vehicle ID you're accessing.
pawn Код:
// In your PlayerInfo enum
enum pInfo
{
CarOwner,
};
new PlayerInfo[MAX_PLAYERS][pInfo];
// Using an example command: /carowner
CMD:carowner(playerid, params[])
{
// Create two variables for the name and string
new name[MAX_PLAYER_NAME], string[50];
// String size 50 because the "Vehicle is owned by" is 20 characters long, and "name" is 24.
// Make the command only usable when playerid is in a vehicle
if(!IsPlayerInAnyVehicle(playerid))
return SendClientMessage(playerid, -1, "You need to be in a vehicle in order to do this command!");
// Loop through all connected players
foreach(new i: Player)
{
// If a player's "CarOwner" variable is the vehicleid
if(PlayerInfo[i][CarOwner] == GetPlayerVehicleID(playerid))
{
// Store their name in the "name" variable
GetPlayerName(i, name, sizeof(name));
}
else return false;
}
// Now format them with a message and send it to playerid
format(string, sizeof(string), "Vehicle is owned by %s", name);
SendClientMessage(playerid, -1, string);
// Return a value
return 1;
}