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.
What is the issue? I know how to use magnitude (If that helps); however, I do not know how to put it into practice.
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!
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.
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
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.
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