How do i detect How many parts are near a player?

How would I detect how many models, parts are near my Avatar, I made a code that detects the nearest Player Avatar, I made it return a function if No Players are near my Avatar, when 2 characters are near my avatar the script works but it also return Character that are 20 studs away which is something I do not want, i only want that script to run if no character are near me.

1 Like

This post seems pretty similar to what you’re looking for, How to get all parts near a player. Hope you can find what you’re looking for within this, if not feel free to ask further.

1 Like

After looking further I found another post that goes a little more in detail about something similar. Here it is, How to find the nearest part(number) the player is sitting next to?

1 Like

Alright ill look into it and reply if i got it working!

1 Like

Well you can loop through the players and then check if their characters are within a given amount of studs using magnitude Vector3 | Documentation - Roblox Creator Hub. Here is a script I wrote for you that should get you started:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")

local studsAway = 15 -- The amount of studs away you want to check.

for i, v in pairs(game.Players:GetPlayers()) do
	if v.Character and v.Character:FindFirstChild("HumanoidRootPart") and v ~= player then
		if (hrp.Position - v.Character.HumanoidRootPart).Magnitude <= studsAway then
			--Do what you want to do with the Character Near Them Here
		end
	end
end
2 Likes

1.make a part around the player’s location origin aka the humanoid root part’s middle
2.detect anyone around the player using magnitude
3.optional if u wanna step up and do plrs in front and around then use raycasting

1 Like

i have that already, i used a magnitude script, the thing is im tryna detect how many players are near and if its 0 to return a Event.

1 Like

okay so u should apply a max studs for the magnitude so it wont get to 20 studs @DUEL120

1 Like

Alright let me explain

I made a script that detects the nearest players near me, i made it if the player are 20 studs away it returns a function, The thing is when a character is near me and a character is far both functions run at the same time.

1 Like

please provide the script and maybe we can help you…

1 Like
if (plr.Character.HumanoidRootPart.Position - humanoid.Parent.HumanoidRootPart.Position).magnitude <= 30 then


							-- Run if players are nea
						else
							-- Run if no players are near
						end

It works if all players are near, but if 1 player is near and 1 player is far, it runs both functions.

1 Like

your saying if its less than “30” not less than 15 or 19

1 Like

does the same with both.

tirty chars

1 Like

hmm ok wait tirtyyyyyyyyyyyyyyyyyyyyy chars

1 Like

try

if (plr.Character.HumanoidRootPart.Position - humanoid.Parent.HumanoidRootPart.Position).magnitude < 20 then

1 Like

idk if u understand lol

So like im tryna check the amount of players near me, if no players are near me then run a function, my code works if a 1 player is near me but if another player is far like 30 studs away it run the no players are near me function.

basically runs both scripts

1 Like

yeah… but its just a reference try adding that to ur script?

1 Like
	for _,players in pairs(game.Players:GetPlayers()) do
		
		
		for _,Character in pairs(game.Workspace:GetChildren()) do
			if Character:IsA("Model") then
				wait()
				for _,CharacterChildren in pairs(Character:GetChildren()) do
					if CharacterChildren:IsA("Humanoid") then
						local humanoid = CharacterChildren
						print(humanoid)
						
						
						if humanoid:IsDescendantOf(plr.Character) then
							return
						end
						
						
						if (plr.Character.HumanoidRootPart.Position - humanoid.Parent.HumanoidRootPart.Position).magnitude <= 30 then


					
							delay(6,function()
								remotes:FireClient(plr)
								self.Active.soulreapEnabled = false
							end)

						else
							remotes:FireClient(plr)
							print("humanoid is far / no humanoid detected")
						end
					end
				end
			end
		end
	end

my script.

Alright

kewklrmewrweeweewewewwe

image_2021-02-03_231949