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

Why can't this script locate multiple enemies at a time?

Asked by 9 years ago

So this script is supposed to be something sort of like a mini-map, but for some reason it can't locate multiple enemies at a time, it only puts 1 enemy on the map at a time. Here it is:

local _FindCharacter = script.Parent.Parent.Parent.Parent.Parent.Character
function ClearMiniMap()
    for i,v in pairs(script.Parent:GetChildren()) do
        if v:IsA("Frame") and v.Name == "Enemy" or v.Name == "Neutral" or v.Name == "Friendly" then
            v:Destroy()
        end
    end 
end
function enemyDetected(pos)
    local x = game.Workspace:children()
    for i=1,#x do
        local xlist = x[i]
        if xlist.className == "Model" and xlist~= script.Parent then
            local islarm = xlist:FindFirstChild("Left Arm")
            local ishuman = xlist:FindFirstChild("Enemy")
            if islarm ~=nil and ishuman~=nil then
                if (xlist.Position - pos).magnitude<50 then
                return xlist    
                end
            end
        end

    end 
end

while wait(4) do
    ClearMiniMap()
    if _FindCharacter ~= nil then
        local target = enemyDetected(_FindCharacter.Torso)
        if target ~= nil then
            if target.Detected == true then
                local CopyEnemy = script.Parent.Storage.Enemy:Clone()
                CopyEnemy.Parent = script.Parent
                CopyEnemy.Position =(_FindCharacter.Torso.Position.x - target.Position.x), (_FindCharacter.Torso.Position.z - target.Position.z)
            end
        end
    end
end

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

Your enemyDetected only returns the first enemy it finds, not every single enemy visible!

To fix this, I made it return a Table of all matches, rather than the first match:

local _FindCharacter = script.Parent.Parent.Parent.Parent.Parent.Character
function ClearMiniMap()
    for i,v in pairs(script.Parent:GetChildren()) do
        if v:IsA("Frame") and v.Name == "Enemy" or v.Name == "Neutral" or v.Name == "Friendly" then
            v:Destroy()
        end
    end 
end

function enemyDetected(pos)
    local detected = {}

    for _, xlist in ipairs(workspace:GetChildren())
        if xlist:IsA("Model") and xlist ~= script.Parent then --Directly accessing the className is not recommended.
            local islarm = xlist:FindFirstChild("Left Arm")
            local ishuman = xlist:FindFirstChild("Enemy")
            local torso = xlist:FindFirstChild("Torso")
            if torso and islarm and ishuman then --`~= nil` is never necessary.
                If (torso.Position - pos).magnitude < 50 then --Models don't have a Position property!
                    table.insert(detected, torso)  
                end
            end
        end
    end 

    return detected
end

while wait(4) do
    ClearMiniMap()
    if _FindCharacter ~= nil then
        local targets = enemyDetected(_FindCharacter.Torso)
        for _, target in ipairs(targets) do
            if target ~= nil then
                if target.Detected == true then
                    local CopyEnemy = script.Parent.Storage.Enemy:Clone()
                    CopyEnemy.Parent = script.Parent

            --I assume this is a GUI element?
                    CopyEnemy.Position = UDim2.new(0, (_FindCharacter.Torso.Position.x - target.Position.x),
                0, (_FindCharacter.Torso.Position.z - target.Position.z))
                end
            end
        end
    end
end
Ad

Answer this question