Help me with a command.
#1

I was working on a command that plays a sound but I tired to make it only when the player who activated the sound could hear it and the players who are nearby him, it just doesn't work
this is my script, this is kinda bad, It doesn't play the sound sometimes to the player, and sometimes to everyone else.
pawn Код:
forward BoomSound( playerid );
public  BoomSound( playerid )
{
new otherId;
  new Float:x, Float:y, Float:z;
 
    if(IsPlayerInAnyVehicle(playerid))
            {
   if(PlayerToPoint(15.0,otherId, x, y, z))

             PlayerPlaySound(playerid, 1147, x, y, z);
  PlayerPlaySound(otherId, 1147, x, y, z);


 
   }
}
Reply
#2

You do not set:
- otherId: it is 0 and will always play the sound for player 0 then
- x/y/z: you can leave it 0, it will play the sound at the players position then, or set it to whatever you want
Reply
#3

So what should I put instead of otherId ? I ? or playerid ? if I, how can I define it ?
Reply
#4

Alright I have edited like this, it only plays the sound for the player who activated, players who stand nearby can't hear.
pawn Код:
forward BoomSound(i);
public  BoomSound(i)
{

 
 
    if(IsPlayerInAnyVehicle(i))
            {
   if(PlayerToPoint(15.0,i, 0, 0, 0))

             PlayerPlaySound(i, 1147, 0, 0, 0);
  PlayerPlaySound(i, 1147, 0, 0, 0);


 
   }
}
Reply
#5

Anyone please I can't solve it please.
Reply
#6

What's the problem?
Reply
#7

Quote:
Originally Posted by Scriptissue
Посмотреть сообщение
I was working on a command that plays a sound but I tired to make it only when the player who activated the sound could hear it and the players who are nearby him, it just doesn't work
this is my script, this is kinda bad, It doesn't play the sound sometimes to the player, and sometimes to everyone else.
pawn Код:
forward BoomSound( playerid );
public  BoomSound( playerid )
{
new otherId;
  new Float:x, Float:y, Float:z;
 
    if(IsPlayerInAnyVehicle(playerid))
            {
   if(PlayerToPoint(15.0,otherId, x, y, z))

             PlayerPlaySound(playerid, 1147, x, y, z);
  PlayerPlaySound(otherId, 1147, x, y, z);


 
   }
}
Ok so:
You want the player who's ID gets called (in BoomSound(playerid)) to hear the sound
Any player within a 15 distance radius to hear it.

Let's start off by getting the player's position and playing the sound to them:
pawn Код:
public BoomSound(playerid){
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid,x,y,z);
    PlayerPlaySound(playerid, 1147, x, y, z);
    return 1;
}
First part done.
Now you need to check every player to see if they are in the radius of 15.
For this, you need a loop. Here is an example:

pawn Код:
for(new i; i<5; i++){
    printf("%d",i);
}
This works by:
Creating the variable "i".
Then, the code between the { and } will get called as long as the second part in for() is true (Is i less than 5).
And at the end of the for(), we have what will happen to i when the loop returns to the start.

This means that the code will print this:
pawn Код:
0
1
2
3
4
So when we need to check every player's distance from a certain player, we can use a loop like this:
pawn Код:
for(new i; i<MAX_PLAYERS; i++)// You don't have to use i, less than or ++ everytime
{
    if(IsPlayerInRangeOfPoint(i,15.0,x,y,z))PlayerPlaySound(i, 1147, x, y, z);
}
The final code:
pawn Код:
public BoomSound(playerid){
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid,x,y,z);
    PlayerPlaySound(playerid, 1147, x, y, z);
    for(new i; i<MAX_PLAYERS; i++)
    {
        if(!IsPlayerConnected(i))continue;
        if(i==playerid)continue;//I added this so the player doesn't get it played twice to them (continue means skip to the end)
        if(IsPlayerInRangeOfPoint(i,15.0,x,y,z))PlayerPlaySound(i, 1147, x, y, z);
    }
    return 1;
}
My tutorial on loops isn't really that good at all, but there are more tutorials for you to look at on the wiki and forums.
Reply
#8

If I use the command the whole server can hear the sound, how can I change so that it will be heard only to the player whosoever stand nearby and not to the whole server ?
this is how the script looks like:
pawn Код:
public BoomSound(playerid){
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid,x,y,z);
    PlayerPlaySound(playerid, 1147, x, y, z);
    for(new i; i<MAX_PLAYERS; i++)
    {
        if(i==playerid)continue;//I added this so the player doesn't get it played twice to them (continue means skip to the end)
        if(IsPlayerInRangeOfPoint(i,15.0,x,y,z))PlayerPlaySound(i, 1147, x, y, z);
    }
    return 1;
}
this is the upper script that activates the sound
pawn Код:
if ( g_BoomTimer[ playerid ] != -1 )
                   {
                    KillTimer( g_BoomTimer[ playerid ] );
                    g_BoomTimer[ playerid ] = -1;
                    }
                    else
                    {
                    g_BoomTimer[ playerid ] = SetTimerEx( "BoomSound", 1000, true, "i", playerid );
                    }
Reply
#9

Hm. How did you test it? Since the player who activates it, and anyone within 15 distance will hear it.

Also, I added
pawn Код:
if(!IsPlayerConnected(i))continue;
To the final code.

Edit: I tested this in-game and it worked fine. Hm.
Reply
#10

It worked, thank you so much, you have helped me to understand a new method.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)