PlayerPlaySound Issue [SOLVED]
#1

Hi, I am trying to create a simple car alarm system which sounds 50% of the time when a players state changes to driver.

When the alarm sounds it is only heard by the person that entered the vehicle, I actually want all nearby players to hear it also. I know my code should only work for the individual player, I just want to know what should be added to make it work as desired.

Here is the code:

Код:
public CarAlarmTimer()
{
  for(new i=0; i<MAX_PLAYERS; i++) {
		if (IsPlayerConnected(i) && AlarmSounded[i] <= 9) {
		  AlarmSounded[i] += 1;
		  if (AlarmSounded[i] <= 9) {
		  	new currentveh;
     		currentveh = GetPlayerVehicleID(i);
     		new Float:vehx, Float:vehy, Float:vehz;
     		GetVehiclePos(currentveh, vehx, vehy, vehz);
    		PlayerPlaySound(i, 1147, vehx, vehy, vehz);
    	} else {
    		KillTimer(CarAlarm);
    		AlarmSounded[i] = 10;
			}
		}
	}
}
Any help or suggestions are welcome, thanks.
Reply
#2

Well I hope you're not just constantly having this running as that would be bad.

pawn Код:
public CarAlarmTimer()
{
  for(new i=0; i<MAX_PLAYERS; i++)
  {
    if (IsPlayerConnected(i))
    {
      if (AlarmSounded[i] <= 9)
      {
        AlarmSounded[i]++;
        new Float:vehx, Float:vehy, Float:vehz;
        GetVehiclePos(GetPlayerVehicleID(i);, vehx, vehy, vehz);
        PlayerPlaySound(i, 1147, vehx, vehy, vehz);
      }
      else
      {
        KillTimer(CarAlarm);
        AlarmSounded[i] = 10;
      }
    }
  }
}
That won't fix your problem, but it is a shorter/better version of your code. I know how to fix your problem with playing it for surrounding players, but first I want to know how you are doing this. Are you constantly having this timer running or just whenever a player enters a locked/owned vehicle?
Reply
#3

This timer is called nearly 50 % of the time when a player enters a vehicle and then stops after 9 loops, I supose that is quite wasteful but it's been a year since i coded anything and could use some extra knowlage. Time to hit the pawn-lang again lol.

This is the code activating it:

Код:
public OnPlayerStateChange(playerid, newstate, oldstate)
{
  if(oldstate == PLAYER_STATE_ONFOOT && newstate == PLAYER_STATE_DRIVER) {

		new rand = random(2000);
	
  	if(rand >= 0 && rand <= 1000) {
			SendClientMessage(playerid, COLOR_WHITE, "The alarm has sounded.");
			AlarmSounded[playerid] = 0;
			PlayerPlaySound(playerid, 1147, 0.0, 0.0, 0.0);
			CarAlarm = SetTimer("CarAlarmTimer", 1000, 1);
 		}
 	
		if(rand >= 1001 && rand <= 2000) {
  			SendClientMessage(playerid, COLOR_WHITE, "The alarm has not sounded.");
		}
}
	return 1;
}
I have PlayerPlaySound here also as a one off to stop the alarms 1 seccond delay.

Edit: Problem solved, thanks to Baked-Banana.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)