Help to create an npc that chase the player

  1. What do you want to achieve?
    I want to make a rig chase the player for my game.

  2. What is the issue?
    So I tried making one but the algorithm usually get confuse. Also I’m trying to make it very reactive and smooth (no delay when I change place) like the others roblox games but fail.

  3. What solutions have you tried so far?
    I tried looking on youtube for scripts and explanation but I couldn’t fix the problem. The scripts of people on youtube looks very similar to mine but works unlike mine.

Video :

Here the script I use :

local Rig = workspace.Hunter
local PathFinding = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local Players = game.Players:GetPlayers()
local Range = 80
local Start = Rig.HumanoidRootPart.Position
local End = workspace.End.Position
local LastTarget



local function Trace(waypoint)
	local Part = Instance.new("Part", workspace)
	Part.Position = waypoint.Position + Vector3.new(0,1,0)
	Part.Size = Vector3.new(3,3,3)
	Part.Anchored = true
	Part.CanCollide = false
	Part.Color = Color3.new(0.447059, 1, 0.0235294)
end
--Rig.Humanoid.JumpPower = 100

local function Recalculate (Target)
	--	print("calculating.. for target : ",Target.Name)
	local Path = PathFinding:CreatePath({
		AgentRadius = 3,
		AgentHeight = 6,
		AgentCanJump = true,
		--Costs = {
		--	--Plastic = math.huge,
		--	--Ice = 0
		--}
	}
	)
	Path:ComputeAsync(Rig.HumanoidRootPart.Position, Target.HumanoidRootPart.Position)
	--	print("calculated")
	return Path
end
local NewAction = false
local TargetIdx = 0
local function Target(target)
	--print("new")
	local Path = Recalculate(target)
	local HRP = target.HumanoidRootPart
	local Waypoints = Path:GetWaypoints()
	
	if Path.Status ~= Enum.PathStatus.Success then return end
	
	for i, waypoint in pairs(Waypoints) do
		print("moving to : ",i)
		
		
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			Rig.Humanoid.Jump = true
		end
		
		
		Rig.Humanoid:MoveTo(waypoint.Position)
		--Trace(waypoint)
		Rig.Humanoid.MoveToFinished:Wait()
	end
	
	
	
	
	
	
	
	
	
	
end

local LastPosition

RunService.Heartbeat:Connect(function()
	local Players = game.Players:GetPlayers()
	for i, plr in ipairs(Players) do
		plr = workspace:FindFirstChild(plr.Name)
		local HRP = plr:FindFirstChild("HumanoidRootPart")
		if not HRP then return end
		if (plr.HumanoidRootPart.Position - Rig.HumanoidRootPart.Position).Magnitude <= Range then
			Target(plr)
			NewAction = true
		end
	end
end)

How can i fix my script ? Does someone has a better way to complete my goal ?

3 Likes

Change Heartbeat to a generic while true do wait() loop, it’s running too fast and glitches the hunter
Also I recommend setting the hunters HRP network ownership for faster moveTos like this

HRP:SetNetworkOwner(nil)
2 Likes

Thanks, I just tried and it worked, but I’m wondering if there’s a way to make it goes faster because it takes time to react (like average game with pathfinding npc) ? Because I already saw lots of videos of people using HeartBeat for this.

1 Like

You’ll need another script as this one is not really suitable for smoothness, I have a video for you here (if you don’t want to go through it download the model in description): https://www.youtube.com/watch?v=ibvoqnG3YqI (not made by me)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.