player = game.Players.LocalPlayer
mouse = player:GetMouse()
function findNearestTorso(pos, maxDistance, randomSelect)
local list = game.Workspace:GetChildren()
local torsos = {}
for x=1, #list do
if (list[x].className == "Model") and (list[x] ~= script.Parent) then -- CHECK1
local torso = list[x]:findFirstChild("Torso")
local human = list[x]:findFirstChild("Humanoid")
if torso ~= nil and human ~= nil and human.ClassName == "Humanoid" and human.Health > 0 then
for i=1,#torsos do
if (torso.Position - pos).magnitude < torsos[i][2] then
table.insert(torsos, i, {torso, (torso.Position - pos).magnitude})
break
end
if i == #torsos and (maxDistance == nil or (torso.Position - pos).magnitude < maxDistance) then
table.insert(torsos, {torso, (torso.Position - pos).magnitude})
end
end
if torsos[1] == nil and (maxDistance == nil or (torso.Position - pos).magnitude < maxDistance) then
torsos[1] = {torso, (torso.Position - pos).magnitude}
end
end
end
end
if randomSelect == nil then
return torsos[1][1], torsos[1][2]
else
if randomSelect > #torsos then randomSelect = #torsos end
local selection = math.random(1, randomSelect)
return torsos[selection][1], torsos[selection][2]
end
end
mouse.KeyDown:connect(function(key)
if key == 'e' then
h = findNearestTorso(player.Character.Torso.Position,20,nil)
print(h.Parent) -- My characther Player1
end end)
This is placed in a localscript in the ScreenGui of the player, it always prints my character, not the nearest one but as you can see at CHECK1 that it should not be my character. And I need help...

1 comment
I can't believe I couldn't see it. Thanks for the help