Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How Do i Find The Closes Humanoid To The Player?

Asked by
VAnkata20 135
2 years ago

I have no idea how to make this work that's why there is no code here

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

I hope this works for ya!

function getClosestHumanoid(player, limit) -- limit defined for how far the function will get the closest humanoid in studs
    local closest, inst = nil, nil
    local char = player.Character or player.CharacterAdded:Wait()

    for i, v in pairs(game.Players:GetPlayers()) do -- loop through the available players
        if v == player then
            continue
        end

        if closest == nil then
            local character = v.Character

            if character and character:FindFirstChild('Humanoid') then
                local magnitude = (char.HumanoidRootPart.Position-character.HumanoidRootPart.Position).Magnitude -- we convert this into magnitude (distance)

                if limit and magnitude <= limit then
                    closest = magnitude
                    inst = character
                elseif not limit then
                    closest = magnitude
                    inst = character
                end
            else
                continue
            end
        else
            local character = v.Character
            local magnitude = (char.HumanoidRootPart.Position-character.HumanoidRootPart.Position).Magnitude

            if character and character:FindFirstChild('Humanoid') and magnitude < closest then

                if limit and magnitude <= limit then
                    closest = magnitude
                    inst = character
                elseif not limit then
                    closest = magnitude
                    inst = character
                end
            else
                continue
            end
        end
    end

    return inst.Humanoid -- remove .Humanoid if you want to get the character object
end
Ad
Log in to vote
0
Answered by 2 years ago

I hope this can help:

-- function to get the closest humanoid to a specific player:

function getClosestHumanoid(plr)
local char = plr.Character

if char then
local closest_char = nil
local closest_pos = nil

-- loops through all the children of workspace:
for _,v in pairs(workspace:GetChildren()) do

local humanoid = v:FindFirstChild("Humanoid")

-- checks if it has a humanoid:
if humanoid then

local root = v.PrimaryPart -- aka "HumanoidRootPart"

-- checks if there is no closest character or the distance between the player and the current humanoid is less than the current closest distance
if  not closest_char or plr:DistanceFromCharacter(root.Position) < closest_pos then

closest_char = v
closest_pos =  plr:DistanceFromCharacter(root.Position)




end



end



end


return closest_char, closest_pos

end


end

Hope this helped a bit

0
it didnt work for me VAnkata20 135 — 2y
0
i am using an event to get the users mause2down VAnkata20 135 — 2y

Answer this question