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 10 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:

01local _FindCharacter = script.Parent.Parent.Parent.Parent.Parent.Character
02function ClearMiniMap()
03    for i,v in pairs(script.Parent:GetChildren()) do
04        if v:IsA("Frame") and v.Name == "Enemy" or v.Name == "Neutral" or v.Name == "Friendly" then
05            v:Destroy()
06        end
07    end
08end
09function enemyDetected(pos)
10    local x = game.Workspace:children()
11    for i=1,#x do
12        local xlist = x[i]
13        if xlist.className == "Model" and xlist~= script.Parent then
14            local islarm = xlist:FindFirstChild("Left Arm")
15            local ishuman = xlist:FindFirstChild("Enemy")
View all 38 lines...

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 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:

01local _FindCharacter = script.Parent.Parent.Parent.Parent.Parent.Character
02function ClearMiniMap()
03    for i,v in pairs(script.Parent:GetChildren()) do
04        if v:IsA("Frame") and v.Name == "Enemy" or v.Name == "Neutral" or v.Name == "Friendly" then
05            v:Destroy()
06        end
07    end
08end
09 
10function enemyDetected(pos)
11    local detected = {}
12 
13    for _, xlist in ipairs(workspace:GetChildren())
14        if xlist:IsA("Model") and xlist ~= script.Parent then --Directly accessing the className is not recommended.
15            local islarm = xlist:FindFirstChild("Left Arm")
View all 46 lines...
Ad

Answer this question