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

How do I create a follow script for an Enemy NPC that choose the users around?

I'm making a RPG game and I don't know how to make an enemy NPC that follow and kill players. If have anyone know that, please help me. I have try to run this script in Roblox Studio but it not do anything. This is my script code I don't know how to fix it:

players = nil; --Where we are going to store all the players
dummy = game.Workspace.Dummy; --Dummy reference
currentPlayerFollowing = nil; --The current player the zombie is following
damage = 2; --Change this for more damage if you want

function touched(hit)
  --pcall allows for errors to be disregarded so you don't have to worry about random output errors
   if pcall(function() if hit.Parent.Humanoid ~= nil then end end) then --If the character's health storage is there
   pcall(function() hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - damage end); --Do damage
   end
end

dummy.Torso.Touched:connect(touched);

while wait(1) do --Forever loop
 players = game.Players:GetChildren(); --Grab all the Players currently in the game
 
 for i = 0, table.getn(players), 1 do --Go through all of the players
  if (players[i] ~= nil and (players[i].Character.Torso.Position - dummy.Torso.Position.X).magnitude <= 10) then --Subtract the distance between the player and the zombie
   currentPlayerFollowing = players[i]; --Add the player to the currentPlayerFollowing reference so the zombie can follow them
  end
 end
 
 if (currentPlayerFollowing ~= nil) then --If the player is there
    dummy.Humanoid.WalkToPoint = currentPlayerFollowing.Character.Torso.Position --Walk to the player
 end
end?

What happend with this? Please help me! Thank for your help.

1 comment

0

You don’t know how to fix it cause it’s not yours.

1 answer

✓ Accepted answer

Try this:

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 45
	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("HumanoidRootPart")
			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




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

end

local dist = 45 --Change the number to the amount of studs the player has to be for the enemy to start chasing!

I'm assuming the enemy humanoid is named (Zombie) If it isn't go to line 34 and cahge Zombie to whatever the humanoid name is.

Hope this helps!

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