SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 04/08/2020, 08:38 PM   #1
murilo2929
Huge Clucker
 
Join Date: Oct 2018
Posts: 234
Reputation: 2
Question Close each other

how could I be making a variable work only if the players are close to each other?
murilo2929 is offline   Reply With Quote
Old 04/08/2020, 10:02 PM   #2
Proxus
Huge Clucker
 
Proxus's Avatar
 
Join Date: May 2018
Location: United Kingdom
Posts: 463
Reputation: 20
Default Re: Close each other

Get the player's position with GetPlayerPos and use this function to check if they're near: IsPlayerInRangeOfPoint.

For example:
Code:
new Float:x, Float:y, Float:z;

// Get the player's position
GetPlayerPos(playerid, x, y, z);

// Check if the player is within 5 metres of the other player
if (IsPlayerInRangeOfPoint(OtherPlayer, 5.0, x, y, z))
{
	// Do stuff
}
__________________
Developer @ Trucknation
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity"


IMPORTANT LINKS
An upcoming mix of trucking & light roleplay
Proxus is offline   Reply With Quote
Old 04/08/2020, 10:35 PM   #3
murilo2929
Huge Clucker
 
Join Date: Oct 2018
Posts: 234
Reputation: 2
Default Re: Close each other

Quote:
Originally Posted by Proxus View Post
Get the player's position with GetPlayerPos and use this function to check if they're near: IsPlayerInRangeOfPoint.

For example:
Code:
new Float:x, Float:y, Float:z;

// Get the player's position
GetPlayerPos(playerid, x, y, z);

// Check if the player is within 5 metres of the other player
if (IsPlayerInRangeOfPoint(OtherPlayer, 5.0, x, y, z))
{
	// Do stuff
}
can i put playerid here?
Code:
if (IsPlayerInRangeOfPoint(playerid, 5.0, x, y, z))
or need to be OtherPlayer
murilo2929 is offline   Reply With Quote
Old 04/08/2020, 10:38 PM   #4
Proxus
Huge Clucker
 
Proxus's Avatar
 
Join Date: May 2018
Location: United Kingdom
Posts: 463
Reputation: 20
Default Re: Close each other

Quote:
Originally Posted by murilo2929 View Post
can i put playerid here?
Code:
if (IsPlayerInRangeOfPoint(playerid, 5.0, x, y, z))
or need to be OtherPlayer
If you get the other player's position then you can put playerid there. You can't get Player1's position and check if he's near them (because it'll always be true because he is Player1). You need to get Player1/Player2's position and check if the other player is near to that position.

As long as the player's position you're retrieving and the player you're checking is near is different then that's fine.
__________________
Developer @ Trucknation
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity"


IMPORTANT LINKS
An upcoming mix of trucking & light roleplay
Proxus is offline   Reply With Quote
Old 04/08/2020, 10:44 PM   #5
murilo2929
Huge Clucker
 
Join Date: Oct 2018
Posts: 234
Reputation: 2
Default Re: Close each other

Quote:
Originally Posted by Proxus View Post
If you get the other player's position then you can put playerid there. You can't get Player1's position and check if he's near them (because it'll always be true because he is Player1). You need to get Player1/Player2's position and check if the other player is near to that position.

As long as the player's position you're retrieving and the player you're checking is near is different then that's fine.
how can i make it to automatically check the id of the other player? and does it work for more than one person?
murilo2929 is offline   Reply With Quote
Old 04/08/2020, 11:10 PM   #6
Proxus
Huge Clucker
 
Proxus's Avatar
 
Join Date: May 2018
Location: United Kingdom
Posts: 463
Reputation: 20
Default Re: Close each other

Quote:
Originally Posted by murilo2929 View Post
how can i make it to automatically check the id of the other player? and does it work for more than one person?
What's the context in this case though? What are you trying to do that requires you to check for a player nearby?

If you need to check if any player is nearby, you could loop through all players and check if any of them are in range (using the same method in my post above, just within the loop).
__________________
Developer @ Trucknation
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity"


IMPORTANT LINKS
An upcoming mix of trucking & light roleplay
Proxus is offline   Reply With Quote
Old 04/08/2020, 11:16 PM   #7
murilo2929
Huge Clucker
 
Join Date: Oct 2018
Posts: 234
Reputation: 2
Default Re: Close each other

Quote:
Originally Posted by Proxus View Post
What's the context in this case though? What are you trying to do that requires you to check for a player nearby?

If you need to check if any player is nearby, you could loop through all players and check if any of them are in range (using the same method in my post above, just within the loop).
so the case is a voice program, I want the player to hear the other one speaking only if he is close, now at the moment they can hear themselves anywhere on the map.

OBS. if you are interested in helping me and want to charge something I pay.
murilo2929 is offline   Reply With Quote
Old 05/08/2020, 10:00 AM   #8
Proxus
Huge Clucker
 
Proxus's Avatar
 
Join Date: May 2018
Location: United Kingdom
Posts: 463
Reputation: 20
Default Re: Close each other

Quote:
Originally Posted by murilo2929 View Post
so the case is a voice program, I want the player to hear the other one speaking only if he is close, now at the moment they can hear themselves anywhere on the map.

OBS. if you are interested in helping me and want to charge something I pay.
It's fairly simple so anyone that takes payment for that is trying to grab your money.
Code:
public OnPlayerText(playerid, text[])
{
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid, x, y, z);
    for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
    {
        if(IsPlayerInRangeOfPoint(i, 5.0, x, y, z))
        {
            SendClientMessage(i, -1, text);
        }
    }
    return 1;
}
This code will send messages to the nearest player only. If you want to make it a command, you can use the majority of it, just cutting out the parts that relate to the OnPlayerText callback.

How this works is that it gets the player's (who is sending the message) position and checks for every player to see if they are within 5 metres of that position. If they are, they are near that player and then it will send them a message.
__________________
Developer @ Trucknation
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity"


IMPORTANT LINKS
An upcoming mix of trucking & light roleplay
Proxus is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Close it Face9000 Scripting Help 0 06/10/2015 10:28 PM
Problem - Gate open/Close ( Every one can open/close ) Fantje Scripting Help 14 07/03/2015 09:48 PM
close KaPuCin Scripting Help 0 16/01/2014 12:13 PM
Will SA-MP ever close down ? gamegodrules General 53 22/10/2009 04:34 PM
Close something Justsmile Help Archive 5 30/04/2009 10:43 AM


All times are GMT. The time now is 06:32 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.