Cant get pathfinding to work and i cant find out how to fix it

Im just trying to make funni zomber npc go towards the closest player but he ended up somehow going in loops and im going insane trying to make it work

Hroot = script.Parent.HumanoidRootPart
RegenPath = false
PathGenerated = false
Hum = script.Parent:FindFirstChildOfClass("Humanoid")
PathfindingService = game:GetService("PathfindingService")
PathEnd = Vector3.new(-40, 16, -18.5)
Path = PathfindingService:CreatePath({
	AgentRadius = 1,
	AgentHeight = 4.5,
	AgentCanJump = true,
	WaypointSpacing = 3
})
for i,v in pairs(script.Parent:GetChildren()) do
	if v:IsA("BasePart") then
		if v:CanSetNetworkOwnership() == true then
			v:SetNetworkOwner(nil)
		end
	end
end
function Walk(Spot)
	print("Walking")
	Hum:MoveTo(Spot)
	print((Spot - script.Parent.HumanoidRootPart.Position).Magnitude)
	if (Spot - script.Parent.HumanoidRootPart.Position).Magnitude > 3.5 then
		Hum.MoveToFinished:wait()
	else
		wait(.4)
	end
end
function GetClosest(SearchLocation)
	print("Looking")
	local found = false
	local LastDist = 10000000000000000000
	local Plr = nil
	for i,v in pairs(game.Players:GetPlayers()) do

		if v.Character ~= nil then
			if v.Character.PrimaryPart ~= nil then
				found = true
				local mag = (SearchLocation - v.Character.PrimaryPart.Position).Magnitude
				if mag < LastDist then
					LastDist = mag
					Plr = v.Character
				end
			end
		end
	end
	print("Looking1")
	if found == true then
		print("Looking2")
		return Plr
	else
		print("Looking3")
	end
end
function PathCalculate()
	if PathEnd ~= nil and PathEnd ~= Vector3.new(0,0,0) then
		RegenPath = true
		PathGenerated = false
		local Plr = GetClosest(script.Parent.HumanoidRootPart.Position)
		if Plr ~= nil then
			Path:ComputeAsync(script.Parent.Torso.Position, Plr.HumanoidRootPart.Position)
		else
			Path:ComputeAsync(script.Parent.Torso.Position, script.Parent.Torso.Position + Vector3.new(math.random(-1,1)*4,math.random(-1,1)*4,math.random(-1,1)*4))
		end
		RegenPath = false
		PathGenerated = true
	end
end
script.PathSwitch:GetPropertyChangedSignal("Value"):Connect(PathCalculate)
print("Pathing")
wait(.5)
script.PathSwitch.Value = true
while true do
	print("Debug1")

	print("Debug2")
	if RegenPath == false and PathGenerated == true then
		print("Debug3")
		PathGenerated = false
		print("Debug4")
		local Wayoints = Path:GetWaypoints()
		print("Debug5")
		script.PathSwitch.Value = not script.PathSwitch.Value
		PathGenerated = false
		RegenPath = false
		print("Debug6")
		local CashMoney = #Wayoints
		if #Wayoints > 4 then CashMoney = 4 end
		if CashMoney ~= 0 then
			for Point = 1,CashMoney do
				print("Debug7")
				if Wayoints[Point].Action == Enum.PathWaypointAction.Jump then
					print("Debug8")
					Hum.Jump = true
					print("Debug9")					
				end
				Walk(Wayoints[Point].Position)
			end
		end
		if Wayoints[4] ~= nil then

		else
			print("NoPath")
			wait(0.005)
		end

	else print("Path is messed up") wait(0.005) end
end

if anyone knows what im butchering here in this mess of code please tell me.