Speedo freaks.
#1

My speedo freaks when more than one player are driving a vehicle. It starts showing all players speed, and that's bad.
I use GameTextForPlayer, to show the speed.

pawn Код:
new Float: VehicleSpeed[MAX_VEHICLES];
pawn Код:
forward UpSpeed();
public UpSpeed()
{
    new Float:pX,Float:pY,Float:pZ;
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
    if(IsPlayerConnected(i))
        {
        if(IsPlayerInAnyVehicle(i))
            {

            GetPlayerPos(i,Float:pX,Float:pY,Float:pZ);

            VehicleSpeed[GetPlayerVehicleID(i)] = floatsqroot(floatpower(floatabs(floatsub(pX,PlayerData[i][OSX])),2)+floatpower(floatabs(floatsub(pY,PlayerData[i][OSY])),2)+floatpower(floatabs(floatsub(pZ,PlayerData[i][OSZ])),2));
           
            VehicleSpeed[GetPlayerVehicleID(i)] = floatround(VehicleSpeed[GetPlayerVehicleID(i)] * 5000 / 1000);
           
            }
        }
    GetPlayerPos(i,Float:pX,Float:pY,Float:pZ);
    PlayerData[i][OSX]=floatround(pX);
    PlayerData[i][OSY]=floatround(pY);
    PlayerData[i][OSZ]=floatround(pZ);
    }
    return 1;
}
pawn Код:
public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if(newstate == 2 && newstate != 3)
    {
    upspeedt = SetTimerEx("UpSpeed",1000,1,"i",playerid);
    VehicleSpeed[GetPlayerVehicleID(playerid)]=0;
    }
}
What is wrong here?
Reply
#2

I really need this.
Reply
#3

There are a lot of logic errors in your script. The main problem is that you are setting that timer every time someone in your server enters a vehicle. So if two people enter a vehicle then that timer is checking for everyone in the server twice a second. And under OnPlayerStateChange, there is no need to see if their newstate equals 2 and not 3 because if it equals 2 it's obviously not 3. I think I fixed the timer and the OnPlayerStateChange problems and removed some unnecessary things. You just need to add SpeedTimer into the enum for PlayerData.

pawn Код:
forward UpSpeed(playerid,vehicleid);
public UpSpeed(playerid,vehicleid)
{
    new Float:pX,Float:pY,Float:pZ;
    GetVehiclePos(vehicleid,Float:pX,Float:pY,Float:pZ);
    VehicleSpeed[vehicleid] = floatsqroot(floatpower(floatabs(floatsub(pX,PlayerData[playerid][OSX])),2)+floatpower(floatabs(floatsub(pY,PlayerData[playerid][OSY])),2)+floatpower(floatabs(floatsub(pZ,PlayerData[playerid][OSZ])),2));
    VehicleSpeed[vehicleid] = floatround(VehicleSpeed[vehicleid] * 5000 / 1000);

    PlayerData[playerid][OSX]=floatround(pX);
    PlayerData[playerid][OSY]=floatround(pY);
    PlayerData[playerid][OSZ]=floatround(pZ);
    return 1;
}

public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if(newstate == 2)
    {
        new vehicleid = GetPlayerVehicleID(playerid);
        VehicleSpeed[vehicleid]=0;
        PlayerData[playerid][SpeedTimer] = SetTimerEx("UpSpeed",1000,1,"ii",playerid,vehicleid);
        return 1;
    }
    if(newstate == 1)
    {
        if(PlayerData[playerid][SpeedTimer] != -1)
        {
            KillTimer(PlayerData[playerid][SpeedTimer]);
            return 1;
        }
    }
    return 1;
}
Try that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)