Smart Chasing NPC Code Review

So I made a smart chasing npc. Although there are some bugs, it still looks great. And of course I used SimplePath to do that. And also modified the code.

Here’s the script.

task.wait(1)
--Import the module so you can start using it
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SimplePath = require(ReplicatedStorage.SimplePath)
local Players = game:GetService("Players")
--Define npc
local Rig = script.Parent

--Create a new Path using the Dummy
local Path = SimplePath.new(Rig)

function activateTool()
	script.Parent["Walkspeed Overdrive"]:Activate()
end

function Follow()
	for _, plr in Players:GetPlayers() do
		local chr = plr.Character
		print(chr)
		local Goal = chr.HumanoidRootPart
		if chr and Rig.PrimaryPart and chr.PrimaryPart then
			if (Rig.PrimaryPart.Position - chr.PrimaryPart.Position).Magnitude then
				Path:Run(Goal)
			else
				Rig.Humanoid:MoveTo(Rig.PrimaryPart.Position)
			end
		end
		if (Rig.PrimaryPart.Position - chr.PrimaryPart.Position).Magnitude < 30 then
			activateTool()
		end
	end
end

while task.wait() do
	Follow()
end
1 Like