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

How do you get a enemy NPC to lock onto (and follow) a player?

Asked by 3 years ago

This is the revised version of my previous question. -- The main problem with the code below is that the battledroid (the enemy npc in my game) will not lock onto the player, but will lock onto a "drooling zombie" (test npc). There's a line of code that checks if a humanoid is there and if it isn't it'll check for a zombie. What can I do to make it so the droid (enemy npc) will lock onto the player? I'll give the code as well as the link to the game below.

(https://www.roblox.com/games/4931576856/Star-Wars-The-Citadel-Challenge)

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 500
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("Right Arm")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end

human = script.Parent:findFirstChild("Humanoid")
if human == nil then human = script.Parent:findFirstChild("Zombie") end

while true do
    wait(1)
    local target = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil then
        human:MoveTo(target.Position, target)
        human.TargetPoint = target.Position
    end

end
0
On line 27 it should lock onto a humanoid but it wont lock onto a player; why is that? mississippiono 0 — 3y

1 answer

Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
3 years ago

Try this

local npc = script.Parent
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")

local plrsHit = {}

local maxDistance = math.huge


npc.Humanoid.Touched:Connect(function(touch)

    if game.Players:GetPlayerFromCharacter(touch.Parent) and not plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] then

        plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] = true

        touch.Parent.Humanoid:TakeDamage(20)

        wait(1)

        plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] = false  
    end
end)


while wait() do

    local plrs = game.Players:GetPlayers()

    local closestHRP

    for i, plr in pairs(plrs) do

        if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") and plr.Character.Humanoid.Health > 0 then

            local hrp = plr.Character.HumanoidRootPart

            local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude


            if not closestHRP then closestHRP = hrp end

            if (hrpOfNPC.Position - closestHRP.Position).Magnitude > distanceBetween then

                closestHRP = hrp

            end
        end
    end

    if closestHRP and (hrpOfNPC.Position - closestHRP.Position).Magnitude <= maxDistance then npc.Humanoid:MoveTo(closestHRP.Position) end
end
0
It didn't chase me or the zombie :( mississippiono 0 — 3y
0
Is it inside the zombie? MattVSNNL 620 — 3y
0
I put it within the battledroid move script mississippiono 0 — 3y
0
You're supposed to put it inside the zomie model MattVSNNL 620 — 3y
Ad

Answer this question