View difference between Paste ID: P5zcL8N5 and QCARhVzA
SHOW: | | - or go back to the newest paste.
1
local NPC = workspace.Dummy
2
function getClosestPlayer()
3
	local closest_player, closest_distance = nil, 200
4
	for i, player in pairs(workspace:GetChildren()) do
5
		if player:FindFirstChild("Humanoid") and player ~= NPC then
6
			local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
7
			if distance < closest_distance then
8
				closest_player = player
9
				closest_distance = distance
10
			end
11
		end
12
	end
13
	return closest_player, closest_distance
14
end
15
while true do
16
	local player, distance = getClosestPlayer()
17
	if player and distance > 10 then
18
		NPC.Humanoid:MoveTo(player.PrimaryPart.Position)
19
		local stopped 
20
		NPC.Humanoid.MoveToFinished:Connect(function()
21
			stopped = true
22
		end)
23
		repeat 
24
			local distance = (NPC.PrimaryPart.Position-player.PrimaryPart.Position).Magnitude
25
			if distance < 10 then
26
				NPC.Humanoid:MoveTo((NPC.HumanoidRootPart.CFrame*CFrame.new(0,0,-3)).p)
27
				break
28
			end
29
			wait()
30
		until stopped == true
31
	else
32
		wait(1)
33
	end
34
	
35
end