Vehicle Spawning Glitch
#1

Hey guys, I took a massive break from scripting sa-mp and have forgotten a lot about it. I recently just started messing around with an old game mode I was editing and I'm not sure if this glitch was around when I used to script but basically the problem is every time a player logs in their vehicle spawns. It's an edit of Ravens Role play script from years ago and. I managed to find the code that runs when a player logs in.
pawn Код:
if(PlayerInfo[playerid][vModel])
    {
        PlayerInfo[playerid][pVeh] = CreateVehicle(PlayerInfo[playerid][vModel], PlayerInfo[playerid][vX],PlayerInfo[playerid][vY],PlayerInfo[playerid][vZ],PlayerInfo[playerid][vA],PlayerInfo[playerid][vC1],PlayerInfo[playerid][vC2],1200);
        ChangeVehiclePaintjob(PlayerInfo[playerid][pVeh], PlayerInfo[playerid][vPJ]);
        if(PlayerInfo[playerid][vLocked]) SetVehicleParamsEx(PlayerInfo[playerid][pVeh], 0, 0, 0, 1, 0, 0, 0);
        for(new i=0; i<14; i++)
        {
            AddVehicleComponent(PlayerInfo[playerid][pVeh], PlayerInfo[playerid][pVehMod][i]);
        }
    }
    if(PlayerInfo[playerid][vVModel])
    {
        PlayerInfo[playerid][pVVeh] = CreateVehicle(PlayerInfo[playerid][vVModel], PlayerInfo[playerid][vVX],PlayerInfo[playerid][vVY],PlayerInfo[playerid][vVZ],PlayerInfo[playerid][vVA],PlayerInfo[playerid][vVC1],PlayerInfo[playerid][vVC2],1200);
        ChangeVehiclePaintjob(PlayerInfo[playerid][pVVeh], PlayerInfo[playerid][vVPJ]);
        if(PlayerInfo[playerid][vVLocked]) SetVehicleParamsEx(PlayerInfo[playerid][pVVeh], 0, 0, 0, 1, 0, 0, 0);
        for(new i=0; i<14; i++)
        {
            AddVehicleComponent(PlayerInfo[playerid][pVVeh], PlayerInfo[playerid][pVVehMod][i]);
        }
    }
So this creates both vehicles owned by the player EVERY TIME they log in. I need a way to check whether there car has already been spawned or not as to not have a new car respawn every time and having a cluttered server. Is anyone able to help me with this please? Thanks guys
Reply
#2

I believe a good fix for this problem is to destroy the players vehicle once they disconnect.
PHP код:
public OnPlayerDisconnect(playeridreason)
{
    
DestroyVehicle(PlayerInfo[playerid][pVeh]);
    return 
1;

Reply
#3

Thanks for the reply Sellize, I did that as a temporary fix and it worked thanks! Now I guess the next step would be to have the vehicles always there. As in, when the player logs in it doesn't create a new one and when they logout it doesn't destroy it. So the vehicle is always in the game right from the start of the server. Any chance I could get some help again please? Thank you.
Reply
#4

Okay the problem here is that the players vehicle is linked with someones playerid. So when the player disconnects and that playerid is no longer present, multiple things could happen. I am not a 100% sure what would happen, as I don't know how your script handles this, but one thing I do know is that the server won't be able to refer to the vehicle's owner if he is disconnected.

What I was thinking is to destroy the players vehicle once he joins the server and then re-create it, thus only having one.
However I am not sure if this will always work as intended. Anyways, slightly edited code:

PHP код:
if(PlayerInfo[playerid][vModel])
        {
            
DestroyVehicle(PlayerInfo[playerid][pVeh]);
            
PlayerInfo[playerid][pVeh] = CreateVehicle(PlayerInfo[playerid][vModel], PlayerInfo[playerid][vX],PlayerInfo[playerid][vY],PlayerInfo[playerid][vZ],PlayerInfo[playerid][vA],PlayerInfo[playerid][vC1],PlayerInfo[playerid][vC2],1200);
        
ChangeVehiclePaintjob(PlayerInfo[playerid][pVeh], PlayerInfo[playerid][vPJ]);
        if(
PlayerInfo[playerid][vLocked]) SetVehicleParamsEx(PlayerInfo[playerid][pVeh], 0001000);
        for(new 
i=0i<14i++)
                {
                    
AddVehicleComponent(PlayerInfo[playerid][pVeh], PlayerInfo[playerid][pVehMod][i]);
                }
        }
        if(
PlayerInfo[playerid][vVModel])
        {
            
PlayerInfo[playerid][pVVeh] = CreateVehicle(PlayerInfo[playerid][vVModel], PlayerInfo[playerid][vVX],PlayerInfo[playerid][vVY],PlayerInfo[playerid][vVZ],PlayerInfo[playerid][vVA],PlayerInfo[playerid][vVC1],PlayerInfo[playerid][vVC2],1200);
        
ChangeVehiclePaintjob(PlayerInfo[playerid][pVVeh], PlayerInfo[playerid][vVPJ]);
        if(
PlayerInfo[playerid][vVLocked]) SetVehicleParamsEx(PlayerInfo[playerid][pVVeh], 0001000);
        for(new 
i=0i<14i++)
                {
                    
AddVehicleComponent(PlayerInfo[playerid][pVVeh], PlayerInfo[playerid][pVVehMod][i]);
                }
        } 
Reply
#5

Yeah I too thought of that but the problem is if its destroying when he joins and then re-creates it. Nomatter where the vehicle is it will respawn at the last spot they parked it. So if someone is driving their vehicle or using it theyll be thrown out and the car will be returned to their parked location. Its not a bad idea other than that happening I guess. But to begin with the car wont spawn until his atleast logged in once. Not on server start.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)