Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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


2 comments

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

0

How would I make the npc know its a player tho? Just replace Zombie with Humanoid??

1 answer

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

it should be a Humaniod at zombie

1 comment

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

This discussion is preserved as it was. New answers, comments, and votes are closed.