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

How to do i prevent my NPC from shooting multiple targets at once?

Asked by 3 years ago

Im making a star wars game and my battle droid is supposed to fire its blaster at the closest target it can find, however if there are multiple targets in its range it will fire at all of them at once. How can i prevent this from happening. I understand why its happening but how can i prevent it?

Here is a video link to show whats i mean: https://screencast-o-matic.com/watch/c3V6VQVoT5t

Here is the code that makes it fire:

01local agroDist = 50
02local parentTorso = script.Parent.Parent.Parent:WaitForChild("Torso")
03local origin = script.Parent
04local gun = script.Parent.Parent
05local selfHuamn = script.Parent.Parent.Parent.Humanoid
06local humanTable = {}
07local torsoTable = {}
08 
09while wait(2) do
10    local target = nil
11    for i,v in pairs(game.Workspace:GetChildren()) do
12        local human = v:FindFirstChild("Humanoid")
13        local torso = v:FindFirstChild("Torso")
14        if human and torso and torso ~= parentTorso and (parentTorso.Position - torso.Position).magnitude <= agroDist then
15            print("player found near by")
View all 41 lines...
0
try making a variable for the target's name, then having check that variable before every fire. super00rocket 27 — 3y

Answer this question