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

How do you get an enemy NPC to follow the player?

Asked by 3 years ago
Edited 3 years ago

Disclaimer (I'm very new to coding within Roblox and 90% of my game code is from the toolbox, and the battledroid I'm using is also from there; hence my being confused on how to fix it)

The Battledroid NPC won't attack me, but will attack a "drooling zombie". All I need for the Battle Droid to do is to follow me because it already does damage upon touching the player, and it will already shoot and damage the player. - - Could other scripts within my game be affecting it's response? I already deleted the spawn protection wall because I thought it might be messing with it. But it's probably the code which I'll add underneath. Thanks for any help!!

(Btw this is the link to the game: 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
drooling zombie is have Humanoid too that why it attack drooling zombie you need to script to make script know you need to make it attack player not just humanoid Trorapantest1 13 — 3y
0
How would I make the npc know its a player tho? Just replace Zombie with Humanoid?? mississippiono 0 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

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

it should be a Humaniod at zombie

0
The [if human == nil then human = script.Parent:findFirstChild("Zombie") end] code checks if its a humanoid and if not then checks for a zombie. What I need is for it to find a player mississippiono 0 — 3y
Ad

Answer this question