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:
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.