Ad
Log in to vote
0

Find nearest torso problem

Asked by
1N0body 206 Moderation Voter
7 years ago
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 answer

Log in to vote
0
Answered by
jobro13 980 Moderation Voter
7 years ago

If this LocalScript is in the ScreenGui of the player, then script.Parent is the ScreenGui. This is not a member of Workspace. You should check at line 7 if list[x] ~= game.Players.LocalPlayer.Character.

0
I can't believe I couldn't see it. Thanks for the help 1N0body 206 — 7y
Ad

Answer this question