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)

01local larm = script.Parent:FindFirstChild("Left Arm")
02local rarm = script.Parent:FindFirstChild("Right Arm")
03 
04function findNearestTorso(pos)
05    local list = game.Workspace:children()
06    local torso = nil
07    local dist = 500
08    local temp = nil
09    local human = nil
10    local temp2 = nil
11    for x = 1, #list do
12        temp2 = list[x]
13        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
14            temp = temp2:findFirstChild("Right Arm")
15            human = temp2:findFirstChild("Humanoid")
View all 38 lines...
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

01local npc = script.Parent
02local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")
03 
04local plrsHit = {}
05 
06local maxDistance = math.huge
07 
08 
09npc.Humanoid.Touched:Connect(function(touch)
10 
11    if game.Players:GetPlayerFromCharacter(touch.Parent) and not plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] then
12 
13        plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] = true
14 
15        touch.Parent.Humanoid:TakeDamage(20)
View all 50 lines...
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