Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Detector that can detect a player if the player is near?

So basically, I'm making this slam down skill type thing, and I need help sensing the player when the player is near. Ik this has to do with something called magnitude, which I don't know much about. Here is a screenshot and a gyazo for you to examine.

Here is the explorer tab of all the scripts and e.t.c. I've been using.

https://gyazo.com/a4e90b721c0e07446548f82039c5bf39

Here is the local script itself, under Starter-Character Scripts.

https://gyazo.com/b57001af2017df490104b3e3feb49e6e

Here is the 6-second vid of it. (It only plays a sound and 2 animations. The sound plays once you reach the ground, and it only activates when you press e, and has a 0.5-sec cooldown.

https://gyazo.com/0657bc7de3a50d8f9c10f0868fdfc35d

Here's my discord: Aurlius #1780 If you have trouble finding me look up "Aurlius" on the ScriptingHelpers Discord.

So basically I just need a detector that detects when the player is at a certain distance. Thanks.

1 comment

0

Please use code blocks and not pictures to show your code.

1 answer

✓ Accepted answer

Question

Detector that can detect a player if the player is near?

Solution

Basis of Solution

Iterate over the players that are currently in game, get their character, and get the distance from their character to your character.

You can get the distance from your character and someone else's character 2 ways.

  • (Position1 - Position2).Magnitude
  • Player:DistanceFromCharacter(Position2)

I personally would use Player::DistanceFromCharacter because you have to make less references on the front-end Lua side.

The documentation for Player::DistanceFromCharacter is available here: https://developer.roblox.com/en-us/api-reference/function/Player/DistanceFromCharacter

Elaboration

You will want to sort the distances from everybody, and then pick the closest one. An easy way to do this is using table.sort. Given the name of table.sort, you would likely want to store the distances in a table! You can pass a function to table.sort which will determine how to sort it. In this case we can just have the closest player be the 1st index.

Pseudo Code Example

local client = --// reference LocalPlayer
local playersIngame = playersService:GetPlayers() --// players in game right now

local nearPlayers = {} --// store players near you
local maxDistance = 50 --// max distance to check for

--// Iterate over the playersIngame to setup nearPlayers array. 
for i, player in players(playersIngame)
	local character = player.Character

	if (character and (player ~= client)) then --// check if character exists and the player is not the client
		local head = character:WaitForChild("Head") --// get head to position of player
		local distance = client:GetDistanceFromCharacter(head.Position) --// get distance

		--// make sure they're within max distance
		if (distance <= maxDistance) then
			--// add new near player to array
			nearPlayers[#nearPlayers + 1] = {
				player = player,
				distance = distance
			}
		end
	end
end

--// Sort the nearPlayers array
table.sort(nearPlayers, function(playerA, playerB)
	return playerA.distance < playerB.distance
end)

--// Get nearest player
local nearest = nearPlayers[1].player

For a more elaborated answers/more documented answers try reading these threads:

https://devforum.roblox.com/t/checking-nearby-humanoids/200393

https://scriptinghelpers.org/questions/54060/how-do-i-use-magnitude-to-find-the-nearest-player-in-the-game

https://devforum.roblox.com/t/what-is-the-quickest-way-to-see-which-player-is-closest-to-a-position/342962

This discussion is preserved as it was. New answers, comments, and votes are closed.