Checking for nearby players or humanoids

Im making a radar kinda thingy for my game to detect other players because it is like a maze horror type of game and I have no clue how I would detect nearby players
Any suggestions or advice?

1 Like

First step: Get all of the player models
Second step: Get the mainpart position
Third step: Use some math and logic to display that as a point in a gui.

I don’t know, but I saw one system, that the measurement in Studs and scripts comes with a coordinate calculation

You can get the position of all current players by using magnitude checks - and then with this you can then decide how you’ll display them on a radar for example.

Firstly you’ll need to actually get all of the players and their distance away from your current player:

local Players = game:GetService('Players')
local LocalPlayer = Players.LocalPlayer

for _,Player in next, Players:GetChildren() do
	local character = Player.Character
	if character and character.Parent and Player ~= LocalPlayer then
		local Magnitude = (LocalPlayer.Character.HumanoidRootPart.Position - character.HumanoidRootPart.Position).magnitude
		print(Magnitude) -- Magnitude is the distance in studs away from your player
	end
end

You can then for example, consider moving it into a function and returning the player position and distance from your current character:

local Players = game:GetService('Players')
local LocalPlayer = Players.LocalPlayer
local DetectionRadius = 10

local function scanPlayers()
	local Nearby = {}
	for _,Player in next, Players:GetChildren() do
		local character = Player.Character
		if character and character.Parent and Player ~= LocalPlayer then
			local Magnitude = (LocalPlayer.Character.HumanoidRootPart.Position - character.HumanoidRootPart.Position).magnitude
			if Magnitude <= DetectionRadius then
				local PlayerInformation = {Player.Name,Magnitude,character.HumanoidRootPart.Position}
				table.insert(Nearby, PlayerInformation)
			end
		end
	end
	return Nearby 
    -- Returns a table of all players within 10 studs. Name, Distance, Exact Position.
end

Or on the flip-side, you could just use some trickery with view-port frames. I also don’t recommend copying my code as I have tendency to never really give elegant solutions when just writing something up in Sublime as an example.

Alternatively there is :GetDistanceFromCharacter() you can use on each player, same again - by looping. However the result will be the exact same. Maybe it has less overhead, never looked into it.

There may be other ways to do it but the long and short is:

  • Get all the players
  • Use a magnitude check, and get their Positions
  • Display wherever you need
5 Likes