Problem with DestroyObject
#1

Hey there guys i'm making my Courier job script but i have a problem here.

pawn Код:
CMD:loadpackages(playerid, params[])
{
    if(Courier[playerid] == 1 && IsPlayerInAnyVehicle(playerid))
    {
    new objectid = CreateObject(2973, 0, 0.9, -0.2, 0, 0, 0);
    new vehicleid = GetPlayerVehicleID(playerid);
    AttachObjectToVehicle(objectid, vehicleid, 0, 0.9, -0.2, 0, 0, 0);
    } else {
        SendClientMessage(playerid, COLOR_LIGHTRED, "You are either not courier or not in any vehicle !");
    }
    return 1;
}

CMD:deliverpackages(playerid, params[])
{
    if(Courier[playerid] == 1)
    {
    DestroyObject(objectid);
    GivePlayerMoney(playerid, 10);
    SendClientMessage(playerid, COLOR_LIGHTRED, "You unloaded a package successfuly and received 10$ for it !");
    }
    return 1;
}
The deliverpackages command should remove the the object i'm attaching to the vehicle in the loadpackages command. How to DestroyObject from the loadpackages command when i trype deliverpackages ?
Reply
#2

Use a global variable. Local variables (i.e. variables declared within a function) are destroyed when the function exits.
Reply
#3

Do what Vince saids.

Example only, it won't work

pawn Код:
new Object; //put at the top of script

//loadpackages command

Object = CreateObject(2973, 0, 0.9, -0.2, 0, 0, 0);

//deliverpackages command

DestroyObject(Object);
Reply
#4

Just to reiterate what Romel and Vince said, here's you're current code updated.

pawn Код:
new packageObjectID=-1;

CMD:loadpackages(playerid, params[])
{
    if(Courier[playerid] == 1 && IsPlayerInAnyVehicle(playerid))
    {
    if(packageObjectID!=-1)DestroyObject(packageObjectID);
    packageObjectID= CreateObject(2973, 0, 0.9, -0.2, 0, 0, 0);
    new vehicleid = GetPlayerVehicleID(playerid);
    AttachObjectToVehicle(objectid, vehicleid, 0, 0.9, -0.2, 0, 0, 0);
    } else {
        SendClientMessage(playerid, COLOR_LIGHTRED, "You are either not courier or not in any vehicle !");
    }
    return 1;
}

CMD:deliverpackages(playerid, params[])
{
    if(Courier[playerid] == 1)
    {
    DestroyObject(packageObjectID);
    packageObjectID=-1;
    GivePlayerMoney(playerid, 10);
    SendClientMessage(playerid, COLOR_LIGHTRED, "You unloaded a package successfuly and received 10$ for it !");
    }
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)