Two players can rent same vehicle
#1

Hello, I've tested this out with my brother and I don't see what's wrong. When another player tries to rent the same vehicle it allows them to, but sometimes it doesn't. It's weird.

I've posted the codes on Pastebin:

http://pastebin.com/HS4cTVPt

Thank you!
Reply
#2

pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
{
    if(VehicleRentID[i] == vehicle)
    {
        format(string, sizeof(string), "%s is currently renting this Faggio.", GetName(i));
        InformationMessage(playerid, string);
        IsEngineRunning(playerid); // Performs a check to determine whether engine is running, followed by a message sent to the player.
        break;
    }
    else
    {
        format(string, sizeof(string), ""WHITE"You can rent this Faggio for "GREEN"$%d"WHITE".\nWould you like to rent it?", RentalPrice);
        ShowPlayerDialog(playerid, RentalDialog, DIALOG_STYLE_MSGBOX, ""YELLOW"Barrona Rental", string, "Rent", "Exit");
        break;
    }
}
This is faulty code part. Basically, you want to loop through all players, and if anyone is already renting this bike, don't allow anybody else to. However, now your code does this:
1. Begin the loop
2. Check if first player's in loop rented vehicle id is the same as the one player is entering
3. If yes, dissalow renting
4. If no, allow renting

So it will only check VehicleRentID for first player. Use this instead:

pawn Код:
if(oldstate == PLAYER_STATE_ONFOOT && newstate == PLAYER_STATE_DRIVER) // Player entered a vehicle as a driver
{
    new engine, lights, alarm, doors, bonnet, boot, objective;
    GetVehicleParamsEx(vehicle, engine, lights, alarm, doors, bonnet, boot, objective);
    if(IsARentVehicle(vehicle)) // Faggio Rent Vehicles (San Fierro Airport)
    {
        if(VehicleRentID[playerid] > 0)
        {
            ErrorMessage(playerid, "You are already renting a vehicle.");
            RemovePlayerFromVehicle(playerid);
            return 1;
        }
        for(new i = 0; i < MAX_PLAYERS; i++)
        {
            if(VehicleRentID[i] == vehicle)
            {
                format(string, sizeof(string), "%s is currently renting this Faggio.", GetName(i));
                InformationMessage(playerid, string);
                IsEngineRunning(playerid); // Performs a check to determine whether engine is running, followed by a message sent to the player.
                return 1;
            }
        }

        format(string, sizeof(string), ""WHITE"You can rent this Faggio for "GREEN"$%d"WHITE".\nWould you like to rent it?", RentalPrice);
        return ShowPlayerDialog(playerid, RentalDialog, DIALOG_STYLE_MSGBOX, ""YELLOW"Barrona Rental", string, "Rent", "Exit");
    }
}
Also, use GetPlayerPoolSize instead of MAX_PLAYERS when looping through all of them. There's also foreach, but it's another dependency, use it only if you want to use more stuff from y_iterate/foreach.
Reply
#3

Thank you so much, it works. +rep
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)