Detect when players go near each other

  1. What do you want to achieve? I want to be able to detect when players go near each other, so if any player goes near any other player something happens.

  2. What is the issue? I know how to use magnitude (If that helps); however, I do not know how to put it into practice.

  3. What solutions have you tried so far? I looked around and I couldn’t really find much on this topic, the best I could find is if a player goes near a part.

Any help on how to do this would be great. Thanks!

3 Likes

I would recommend using magnitude for this, as you noted you are aware of how to use magnitude. For this I would recommend iterating through all the players characters using a Generic for loop and then check the distance between each players character.

Thanks I’ll have a go at that!

I’ve tried for a while now and the furthest I could get is a script that loops through the players when someone joins:

local Players = game:GetService(“Players”)

local function onplayeradded(player)

print(“Player joined”)

end

for _, player in pairs(Players:GetPlayers()) do

onplayeradded(player)

end

Players.PlayerAdded:Connect(onplayeradded)

local function check()
	for i, plr in ipairs(game:GetService("Players"):GetPlayers()) do
		local char = plr.Character or plr.CharacterAdded:Wait()
		local hrp = char.HumanoidRootPart
		--Check if the humanoidRootPart is near anyone else
		for i,plr2 in ipairs(game:GetService("Players"):GetPlayers()) do
			local otherChar = plr2.Character or plr2.CharacterAdded:Wait()
			local otherHrp = otherChar.HumanoidRootPart
			if (hrp.Position - otherHrp.Position).magnitude < 20 then
				print(char.Name .. " is near to " .. otherChar.Name)
			end
		end
	end
end

Something like that probably.

1 Like

Hi, sorry for the late reply. Does this go in a server script in workspace? Also should it work just as you showed or does it need changing? Thanks

Yeah this doesn’t do anything. I’m not sure if I’m doing something wrong.

Assuming @GEILER123456’s function works, you have to call the function. Let’s say you want this to run every second forever in your game. Just do this:

local function check()
	for i, plr in ipairs(game:GetService("Players"):GetPlayers()) do
		local char = plr.Character or plr.CharacterAdded:Wait()
		local hrp = char.HumanoidRootPart
		--Check if the humanoidRootPart is near anyone else
		for i,plr2 in ipairs(game:GetService("Players"):GetPlayers()) do
			local otherChar = plr2.Character or plr2.CharacterAdded:Wait()
			local otherHrp = otherChar.HumanoidRootPart
			if (hrp.Position - otherHrp.Position).magnitude < 20 then
				print(char.Name .. " is near to " .. otherChar.Name)
			end
		end
	end
end

while true do
    check()
    wait(1)
end

Place it in a script inside of ServerScriptService and you should be good to go.

Also, here’s a post I made on magnitude a few months ago in case you’re interested in how it works internally (I’ll probably make a better update on it later): How does magnitude and range work? - #6 by MJTFreeTime

1 Like

The distance between the players

Create Sparkles when players are close to each other, less than 25

This code when placed inside a Script in StarterPlayer in StarterCharacterScripts

local Myroot = script.Parent:WaitForChild("HumanoidRootPart")
local Radius = 25
local Enable = false

local head = script.Parent:WaitForChild("Head")					
local sparkles = Instance.new("Sparkles")
sparkles.Parent = head
sparkles.SparkleColor = Color3.new(1, 0, 0)
sparkles.Enabled = false

while true do
wait()
Enable = false	
	
local children = workspace:GetChildren()
for i = 1, #children do	
	if children[i].Name ~= script.Parent.Name then
	local humanoid = children[i]:FindFirstChild("Humanoid")		
	local root = children[i]:FindFirstChild("HumanoidRootPart")
	if root ~= nil and humanoid ~= nil and humanoid.Health > 0 then	
	local Distance = (Myroot.Position - root.Position).magnitude		
	if Distance < Radius then		
       Enable = true			
	end			
	end		
	end
end	
	
if Enable == true then
sparkles.Enabled = true
end		
if Enable == false then
sparkles.Enabled = false
end		
	
end

My friends and I had tried the code on the server and it was working fine

10 Likes

Aaaa, it works. Thank you so much! :smile:

1 Like

You’re welcome.Glad I could help :grinning:

2 Likes

Nice, sparkles can be replaced with changing in a local players gui, or much other things. A great example.

Sorry, I did not understand what you mean, I do not speak English

the translation is unclear. Try to speak more widely

Have each sentence on a separate line translate a separate line better for me

Ok.
I am sorry.
What I was saying was, the tutorial you made was very good.
It was easy to understand.
And it can also be customized.

2 Likes

Thank you very much

If you need help, don’t hesitate

You can contact me here on the Developer Forum

I know this forum is very old, but

You sir is a very nice person.

Thank you for the help.

1 Like

You’re welcome. Glad I could help

1 Like