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

Function gets the closest player but ignores the fact that it is behind a wall?

Asked by
PolyyDev 214 Moderation Voter
6 years ago
function FindVisPlayer()
    local closestPlr
    local closestMag = math.huge
    for i,v in next,workspace:GetChildren() do
        --local p = game.Players:GetPlayerFromCharacter(v)
        if v:FindFirstChild("Humanoid") then
            local closeChar = v
            local ray = Ray.new(char.HumanoidRootPart.Position,(closeChar.HumanoidRootPart.Position-char.HumanoidRootPart.Position).unit*1000)
            local ignore = {}
            for _,k in pairs(closeChar:GetDescendants()) do
                if k:IsA("BasePart") and k.Name ~= "HumanoidRootPart" then
                    table.insert(ignore,k)
                end
            end
            for _,k in pairs(char:GetDescendants()) do
                if k:IsA("BasePart") and k.Name ~= "HumanoidRootPart" then
                    table.insert(ignore,k)
                end
            end
            local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,ignore)
            print(hit)
            if v:FindFirstChild("Humanoid") and v.Humanoid.Health > 0 and hit ~= nil then
                local mag = (char.Head.Position-v.Head.Position).magnitude
                --local isEnemy = Enemy(p)
                if v ~= char then
                    if mag < closestMag  then
                        closestMag = mag
                        closestPlr = v
                    end
                end
            end
        end
    end

    return closestPlr,closestMag
end

I need that function to return the closest player that is not behind a wall, it gets the closest player but ignores the fact that it is behind a wall.

Help appreciated

0
Don't worry about commented things PolyyDev 214 — 6y

Answer this question