Find closest player from myself

Hi i need to find the first player instance to myself and i do not have a clue how
there is probably a answer to this already i cant find so help will be greatly apricated
thank you

3 Likes

Use distancefromcharacter , works for me

Iterate through all characters and calculate the magnitude between your HRP and theirs. Then get the character with the closest distance.

You need to set a distance to inf first with a closepart value to nil
then use for loop on a part’s folder then check if the part is near to us, so if it’s near, we gonna set a closet distance and a closetpart to that part, now wait for the loop to done then you finally got it

local Player = game.Players.LocalPlayer
for i, v in pairs(game.Players:GetPlayers()) do
	if v.Character and not Player.Character then
		
		local Distance = Player:DistanceFromCharacter(v.Character.Humanoid)
	end
end

Not sure if this works i havent tested it

You can use magnitude since it can be used to calculate the distance between two positions and would work in many other instances.

(Position1 - Position2).magnitude -- Distance from two positions

Indirectly it does have that as a rule:
About the Scripting Support category

If you can’t provide code, provide methods and other things you’ve tried, as a bare minimum.

But I do think the OP could’ve elaborated on the issue a little more, or at least showed some more effort.

1 Like

thanks for telling me. ill delete that reply.

No need! Many people forget to read the first pinned post.

local PlayerDistances = { }
local LocalPlayer = game.Players.LocalPlayer

for _, player in pairs(game.Players:GetPlayers()) do
    if player ~= LocalPlayer then
        local Character = player.Character
        local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
        local LocalPlayerCharacter = LocalPlayer.Character
        local LocalPlayerHumRootPart = LocalPlayerCharacter:FindFirstChild("HumanoidRootPart")
        
       local Magnitude = (HumanoidRootPart.Position - LocalPlayerHumRootPart.Position ).Magnitude
       table.insert(PlayerDistances, Magnitude)
    end
end

print(math.min(unpack(PlayerDistances)))
1 Like

I need help with scripting is that what scripting support is for?

What if you dont know and just need help

then you should try to atleast learn the basics of lua, watch some tutorials or read the dev hub

1 Like