How to get vehicle id on spawn?
#1

I'm trying to create a car ownership system. I want to when a player spawns and he owns a car to get the car spawned and to get the vehicleid of that car and to assign it to only one player (so when other player who is not an owner of that car gets kicked out from the car). I tried to make it but when an owner of the car enters in his own car he doesn't get a message saying "Welcome to your car " and when other player who is not an owner of that car he doesn't get kicked out. I think it's something wrong with my variable (or whatever the name of it is).

Here's the code:

pawn Код:
#include <a_samp>
#include <zcmd>
#include <djson>

#define OWNED_CARS_COUNT 5
#define PLAYER_PATH "users/%s.json"
new OWNED_CARS = 1;
new pPlayerCarOwned[MAX_PLAYERS] = 0;
new playercar[MAX_PLAYERS];


main()
{
    print("\n----------------------------------");
    print(" Blank Gamemode by your name here");
    print("----------------------------------\n");
}


public OnGameModeInit()
{
    // Don't use these lines if it's a filterscript
    djson_GameModeInit();
    SetGameModeText("Blank Script");
    AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
    return 1;
}

public OnGameModeExit()
{
    djson_GameModeExit();
    return 1;
}

public OnPlayerRequestClass(playerid, classid)
{
    SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
    SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
    SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
    return 1;
}

public OnPlayerConnect(playerid)
{
    new playername[MAX_PLAYERS], file[50], file2[50];
    GetPlayerName(playerid, playername, sizeof(playername));
    format(file, sizeof(file), "vehicles/%d.json", 2);
    format(file2, sizeof(file2), "users/%s.json", playername);
    if(djInt(file2, "vehicle/ID") == 2)
    {
    playercar[playerid] = CreateVehicle(451, djFloat(file, "vehicle/x"), djFloat(file, "vehicle/y"), djFloat(file, "vehicle/z"),90,5,7,5000);
    }
}

public OnPlayerDisconnect(playerid, reason)
{
    playercar[playerid] = 12345;
    return 1;
}

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if(GetPlayerVehicleID(playerid) == playercar[playerid])
    {
    SendClientMessage(playerid, 0xffffff, "Welcome to your CAR :D");
    }
    else
    {
    RemovePlayerFromVehicle(playerid);
    }
}

public OnPlayerExitVehicle(playerid, vehicleid)
{
    pPlayerCarOwned[playerid] = 0;
    return 1;
}
This is just a test script, there's only one car file named "2.json". I want to figure it out before making a real script. Thanks in advance.
Reply
#2

pawn Код:
if(GetPlayerVehicleID(playerid) == playercar[playerid])
That's probably the worst missuse I have ever seen.
Reply
#3

Quote:
Originally Posted by hanzen
Посмотреть сообщение
pawn Код:
if(GetPlayerVehicleID(playerid) == playercar[playerid])
That's probably the worst missuse I have ever seen.
Yeah okay, can you help me out to fix that?
Reply
#4

pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if(vehicleid != playercar[playerid]) return RemovePlayerFromVehicle(playerid);
    else if(vehicleid == playercar[playerid]) return SendClientMessage(playerid, 0xffffff, "Welcome to your CAR :D");
    return 1;
}
Reply
#5

It works for the owner when he gets in the car, but when I connect with different name to the server, I don't get kicked out neither I get "Welcome to your car " message.

EDIT: It works when I put it under OnPlayerUpdate but I get spammed with "Welcome to your car " message when an owner enters a car.
Reply
#6

Put it under OnPlayerStateChange, do a 'if' check for newstate==PLAYER_STATE_DRIVER

example:
pawn Код:
public OnPlayerStateChange(playerid,newstate,oldstate)
{
    if(newstate==PLAYER_STATE_DRIVER)
    {
        //Your code
    }
    return 1;
}
Reply
#7

Quote:
Originally Posted by SilentHuntR
Посмотреть сообщение
Put it under OnPlayerStateChange, do a 'if' check for newstate==PLAYER_STATE_DRIVER

example:
pawn Код:
public OnPlayerStateChange(playerid,newstate,oldstate)
{
    if(newstate==PLAYER_STATE_DRIVER)
    {
        //Your code
    }
    return 1;
}
Thanks for help, I already found the same solution by searching around the forums. It works. Now I gotta figure out how to lock it xD
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)